blob: 4c214f43ce8fd5f840c9cc82fc696841c56b2cae [file] [log] [blame]
Imre Kisa21712e2019-10-08 12:56:59 +02001User guide
2==========
3
4This page describes how to get started with compiling and running unit tests on the host machine.
5
6Host machine requirements
7-------------------------
8
9The system has been successfully tested on the following platforms:
10
11- Ubuntu 19.04
12- Ubuntu 18.04
13- Arch Linux
14- MSYS2 MinGW64
15
16Tools
17-----
18
19The following applications are expected to be installed in the build machine:
20
21- CMake >=3.11
22
23- Python >=3.4
24
25- c-picker
26
27 - pyyaml
28
29 - clang (pip package if not included in libclang)
30
31 - libclang
32
33- git
34
35- Toolchain
36
37- Native build system
38
39Ubuntu 19.04
40^^^^^^^^^^^^
41
42On Ubuntu 19.04 use the following commands to install the required tools:
43
44::
45
46 sudo apt-get install cmake git python3 python3-pip libclang-dev build-essential
47 sudo pip3 install git+[TBD]
48
49Ubuntu 18.04
50^^^^^^^^^^^^
51
52The official Ubuntu 18.04 package repository only contains CMake 3.10 which not satisfies the requirements for building unit
53tests. Fortunately there's an official CMake APT repository provided by Kitware: https://apt.kitware.com/ By adding this server
54to the repository list an up-to-date version of CMake can be installed on Ubuntu 18.04.
55
56::
57
58 wget -O - https://apt.kitware.com/keys/kitware-archive-latest.asc \
59 2>/dev/null | sudo apt-key add -
60 sudo apt-add-repository 'deb https://apt.kitware.com/ubuntu/ bionic main'
61
62 sudo apt-get install cmake git python3 python3-pip libclang-dev build-essential
63 sudo pip3 install git+[TBD]
64
65Arch Linux
66^^^^^^^^^^
67
68On Arch Linux use the following commands to install the required tools:
69
70::
71
72 sudo pacman -Sy cmake git python python-pip python-yaml make gcc clang
73 sudo pip install git+[TBD]
74
75MSYS2 MinGW64
76^^^^^^^^^^^^^
77
78::
79
80 pacman -Sy mingw-w64-x86_64-cmake git mingw-w64-x86_64-python3 \
81 mingw-w64-x86_64-python3-pip make mingw-w64-x86_64-gcc mingw-w64-x86_64-clang
82 pip install git+[TBD]]
83
84
85Integrating firmware test builder into a project
86------------------------------------------------
87
88The firmware test builder can be simply integrated into a CMake based project by adding the lines below to its CMakeLists.txt.
89This example shows how to fetch the firmware test builder files on demand using CMake's FetchContent module.
90
91::
92
93 FetchContent_Declare(
94 firmware_test_builder
95 GIT_REPOSITORY ${FIRMWARE_TEST_BUILDER_URL}
96 GIT_TAG ${FIRMWARE_TEST_BUILDER_REFSPEC}
97 GIT_SHALLOW TRUE
98 )
99
100 FetchContent_GetProperties(firmware_test_builder)
101 if(NOT firmware_test_builder_POPULATED)
102 message(STATUS "Fetching Firmware Test Builder")
103 FetchContent_Populate(firmware_test_builder)
104 endif()
105
106 # Appending Firmware Test Builder's cmake directory to CMake module path
107 list(APPEND CMAKE_MODULE_PATH ${firmware_test_builder_SOURCE_DIR}/cmake)
108
109For more details on adding tests see :ref:`Implementing tests` chapter.
110
111
112Building unit tests
113-------------------
114
115Building unit tests start with running CMake which will check all the prerequisites and generate the native build system's input
116files. This example uses Unix Makefiles. Unit tests exercise the code directly by compiling it into the same binary as the test
117code.
118
119::
120
121 cd project
122 mkdir build
123 cd build
124 cmake -G"Unix Makefiles" ..
125
126After running the previous steps the makefiles are generated into the build directory so make can build unit tests. During unit
127test development if only the source files have changed it's not necessary to re-run cmake it's only needed to run make as shown
128below.
129
130::
131
132 make -j
133
134For building single unit test suites the test's name can be used as a makefile target. Let's assume there's a test suite called
135best_unit_tests.
136
137::
138
139 make best_unit_tests
140
141
142Running unit tests
143------------------
144
145CMake provides a built-in tool called ctest for running all the tests using a single command. It is also able to filter tests or
146run them in parallel for speeding up the tests process. Run all the tests using the following command:
147
148::
149
150 ctest
151
152Each unit test suite has its own executable. The easiest way of running single test suite is running it as a simple executable.
153
154::
155
156 ./best_unit_tests
157
158
159Debugging unit tests
160--------------------
161
162As it was mentioned in the previous section test suites are basically separate executables so they can be debugged as any other
163native applications on the host machine. In a Linux environment gdb or IDE's built-in debugger can be utilized for debugging.
164
165::
166
167 gdb ./best_unit_tests
168
169
170Measuring code coverage
171-----------------------
172
173Inspecting code coverage is a useful method for detecting parts of the code which is not exercised by tests. The build system
174includes an option for generating code coverage report of the unit tests. The coverage is processed by ``lcov`` which needs to
175be installed for this feature. Also the coverage measurement in only available when GCC is used as a compiler.
176
177The methods for enabling coverage measurement is project dependent.
178
179Before collecting coverage info and generating reports the tests must be run as the coverage is a runtime measurement. See
180section `Running unit tests`_ for more information about running unit tests.
181
182In case of enabled coverage report the system adds two new build targets called ``coverage`` and ``coverage_report``. They can
183be used simply by running the following commands if ``make`` is used as a build system.
184
185::
186
187 make coverage
188 make coverage_report
189
190The ``coverage`` target generates lcov info files for further processing. If there are coverage files available from different
191sources (i.e. coverages of other tests) they can be merged with the unit test coverage file and evaluated together.
192
193The ``coverage_report`` target generates a HTML report from the coverage info files. The coverage reports can be found in the
194build directory. The report shows the directory structure of the code and each file can be inspected individually. Line,
195function and branch coverage is included.
196
197
198--------------
199
200*Copyright (c) 2019-2021, Arm Limited. All rights reserved.*
201SPDX-License-Identifier: BSD-3-Clause