Unit exams are our first line of protection towards regressive code modifications.
It gives software program builders quick suggestions about their code at a fine-grained stage.
On this article, I’ll reveal how straightforward it’s so as to add unit exams to your C/C++ challenge with google take a look at
.
Let’s have a look at a easy instance of calculating the imply worth out of an array of integers.
calculate_mean
takes as enter an array of integers and its size; it returns as output the imply of the array (the sum of the array divided by its size) as a float quantity.
File Construction
+ Root
+ modules
- calculations.c
- calculations.h
- CMakeLists.txt
+ exams
- test_calculations.cpp
- CMakeLists.txt
- mainapp.c
- CMakeLists.txt
- conanfile.txt
Observe: all the code for this demo may be discovered on my GitHub account.
modules/calculations.c
It is a module liable for calculations:
Very simple.
It has a perform to calculate the sum of the numbers, and it’s referred to as on the calculate_mean
perform, then the division between the sum and the size is returned.
Now let’s eat this code within the mainapp:
mainapp.c
And now we’d like two CMakeLists.txt
information: one for the mainapp
and one for the modules:
CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)
challenge(MyProject)add_subdirectory(modules)
add_executable($PROJECT_NAME mainapp.c)
target_link_libraries($PROJECT_NAME calculations)
modules/CMakeLists.txt
challenge(calculations) add_library(calculations calculations.c calculations.h)
Briefly, the modules CMakeLists generates a library referred to as calculations
and the principle CMakeLists consumes the library and hyperlinks it to the principle executable.
Now, let’s compile and run.
Compile:
cmake --build ./construct --config Debug --target MyProject -j 10 --
Run:
./construct/MyProject
The output is:
Imply=5.78
Google test, or gtest
is an open supply framework for unit testing CC++ initiatives.
It simply integrates with CMake, has an awesome assertion engine, and produces XML experiences to be for show in order that it may be built-in with widespread CICD frameworks.
Step 1. set up gtest from Conan
To be taught extra about Conan, learn my article here.
Let’s create the conanfile.txt
within the root listing:
[requires]gtest/cci.20210126[generators]cmake
run — conan set up . -pr=myprofile
Step 2. add gtest to CMakeLists
Having gtest put in, let’s add it as a dependency to the principle CMakeLists file:
cmake_minimum_required(VERSION 3.10.2)challenge(MyProject)embrace($CMAKE_SOURCE_DIR/conanbuildinfo.cmake)conan_basic_setup()add_subdirectory(modules)add_subdirectory(exams)add_executable($PROJECT_NAME mainapp.c)target_link_libraries($PROJECT_NAME calculations)
We’ve added 4 strains, one to incorporate the configurations by Conan, two to run the Conan CMake arrange, and one so as to add the exams
listing.
Step 3. write the take a look at suite
exams/test_calculations.cpp
That is the precise take a look at case to be executed.
In complete there are 5 take a look at circumstances overlaying varied potential eventualities.
Step 4. configure exams executable with CMake
exams/CMakeLists.txt
cmake_minimum_required(VERSION 3.10.2)challenge(exams)add_executable($PROJECT_NAME test_calculations.cpp)set(CMAKE_CXX_STANDARD 11)target_link_libraries($PROJECT_NAME PUBLICcalculationsgtestgtest_main)
The directions listed below are to create an executable referred to as exams
with three linked libraries, calculations
(the module we wish to take a look at), gtest
and gtest_main
.
Step 5. Run your exams
Compile:
cmake --build ./construct --config Debug --target exams -j 10 --
Run:
buildbintests.exe
Output:
[==========] Operating 5 exams from 1 take a look at suite.
[----------] World take a look at setting set-up.
[----------] 5 exams from test_calculations
[ RUN ] test_calculations.simple_arr
[ OK ] test_calculations.simple_arr (0 ms)
[ RUN ] test_calculations.empty_arr
[ OK ] test_calculations.empty_arr (0 ms)
[ RUN ] test_calculations.all_negatives
[ OK ] test_calculations.all_negatives (0 ms)
[ RUN ] test_calculations.mix_negative_positive
[ OK ] test_calculations.mix_negative_positive (0 ms)
[ RUN ] test_calculations.with_zeros
[ OK ] test_calculations.with_zeros (0 ms)
[----------] 5 exams from test_calculations (70 ms complete)[----------] World take a look at setting tear-down
[==========] 5 exams from 1 take a look at suite ran. (107 ms complete)
[ PASSED ] 5 exams.
Superior! All 5 exams acquired executed and handed!
Now let’s run it once more and export the outcomes to output.xml
:
construct/bin/exams --gtest_output=xml:output.xml
The XML output seems like this:
<?xml model="1.0" encoding="UTF-8"?><testsuites exams="5" failures="0" disabled="0" errors="0" time="0.083" timestamp="2022-02-16T12:57:20.151" title="AllTests"><testsuite title="test_calculations" exams="5" failures="0" disabled="0" skipped="0" errors="0" time="0.055" timestamp="2022-02-16T12:57:20.166"><testcase title="simple_arr" standing="run" end result="accomplished" time="0" timestamp="2022-02-16T12:57:20.171" classname="test_calculations" /><testcase title="empty_arr" standing="run" end result="accomplished" time="0" timestamp="2022-02-16T12:57:20.181" classname="test_calculations" /><testcase title="all_negatives" standing="run" end result="accomplished" time="0" timestamp="2022-02-16T12:57:20.192" classname="test_calculations" /><testcase title="mix_negative_positive" standing="run" end result="accomplished" time="0" timestamp="2022-02-16T12:57:20.204" classname="test_calculations" /><testcase title="with_zeros" standing="run" end result="accomplished" time="0" timestamp="2022-02-16T12:57:20.216" classname="test_calculations" /></testsuite></testsuites>
Let’s load this into codebeautify’s xunit viewer:
Stunning!
This may be simply printed as a take a look at end result in your ci/cd pipelines :).