Imre Kis | a21712e | 2019-10-08 12:56:59 +0200 | [diff] [blame] | 1 | User guide |
| 2 | ========== |
| 3 | |
| 4 | This page describes how to get started with compiling and running unit tests on the host machine. |
| 5 | |
| 6 | Host machine requirements |
| 7 | ------------------------- |
| 8 | |
| 9 | The 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 | |
| 16 | Tools |
| 17 | ----- |
| 18 | |
| 19 | The 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 | |
| 39 | Ubuntu 19.04 |
| 40 | ^^^^^^^^^^^^ |
| 41 | |
| 42 | On 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 | |
| 49 | Ubuntu 18.04 |
| 50 | ^^^^^^^^^^^^ |
| 51 | |
| 52 | The official Ubuntu 18.04 package repository only contains CMake 3.10 which not satisfies the requirements for building unit |
| 53 | tests. Fortunately there's an official CMake APT repository provided by Kitware: https://apt.kitware.com/ By adding this server |
| 54 | to 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 | |
| 65 | Arch Linux |
| 66 | ^^^^^^^^^^ |
| 67 | |
| 68 | On 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 | |
| 75 | MSYS2 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 | |
| 85 | Integrating firmware test builder into a project |
| 86 | ------------------------------------------------ |
| 87 | |
| 88 | The firmware test builder can be simply integrated into a CMake based project by adding the lines below to its CMakeLists.txt. |
| 89 | This 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 | |
| 109 | For more details on adding tests see :ref:`Implementing tests` chapter. |
| 110 | |
| 111 | |
| 112 | Building unit tests |
| 113 | ------------------- |
| 114 | |
| 115 | Building unit tests start with running CMake which will check all the prerequisites and generate the native build system's input |
| 116 | files. This example uses Unix Makefiles. Unit tests exercise the code directly by compiling it into the same binary as the test |
| 117 | code. |
| 118 | |
| 119 | :: |
| 120 | |
| 121 | cd project |
| 122 | mkdir build |
| 123 | cd build |
| 124 | cmake -G"Unix Makefiles" .. |
| 125 | |
| 126 | After running the previous steps the makefiles are generated into the build directory so make can build unit tests. During unit |
| 127 | test 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 |
| 128 | below. |
| 129 | |
| 130 | :: |
| 131 | |
| 132 | make -j |
| 133 | |
| 134 | For 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 |
| 135 | best_unit_tests. |
| 136 | |
| 137 | :: |
| 138 | |
| 139 | make best_unit_tests |
| 140 | |
| 141 | |
| 142 | Running unit tests |
| 143 | ------------------ |
| 144 | |
| 145 | CMake provides a built-in tool called ctest for running all the tests using a single command. It is also able to filter tests or |
| 146 | run them in parallel for speeding up the tests process. Run all the tests using the following command: |
| 147 | |
| 148 | :: |
| 149 | |
| 150 | ctest |
| 151 | |
| 152 | Each 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 | |
| 159 | Debugging unit tests |
| 160 | -------------------- |
| 161 | |
| 162 | As it was mentioned in the previous section test suites are basically separate executables so they can be debugged as any other |
| 163 | native 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 | |
| 170 | Measuring code coverage |
| 171 | ----------------------- |
| 172 | |
| 173 | Inspecting code coverage is a useful method for detecting parts of the code which is not exercised by tests. The build system |
| 174 | includes an option for generating code coverage report of the unit tests. The coverage is processed by ``lcov`` which needs to |
| 175 | be installed for this feature. Also the coverage measurement in only available when GCC is used as a compiler. |
| 176 | |
| 177 | The methods for enabling coverage measurement is project dependent. |
| 178 | |
| 179 | Before collecting coverage info and generating reports the tests must be run as the coverage is a runtime measurement. See |
| 180 | section `Running unit tests`_ for more information about running unit tests. |
| 181 | |
| 182 | In case of enabled coverage report the system adds two new build targets called ``coverage`` and ``coverage_report``. They can |
| 183 | be 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 | |
| 190 | The ``coverage`` target generates lcov info files for further processing. If there are coverage files available from different |
| 191 | sources (i.e. coverages of other tests) they can be merged with the unit test coverage file and evaluated together. |
| 192 | |
| 193 | The ``coverage_report`` target generates a HTML report from the coverage info files. The coverage reports can be found in the |
| 194 | build directory. The report shows the directory structure of the code and each file can be inspected individually. Line, |
| 195 | function and branch coverage is included. |
| 196 | |
| 197 | |
| 198 | -------------- |
| 199 | |
| 200 | *Copyright (c) 2019-2021, Arm Limited. All rights reserved.* |
| 201 | SPDX-License-Identifier: BSD-3-Clause |