Introduction to Google Test: An Open Source C/C++ Unit-Testing Framework | by Eldad Uzman | Feb, 2022

Combine unit exams simply in your initiatives

Photograph by Mitchell Luo on Unsplash

File Construction

+ Root
+ modules
- calculations.c
- calculations.h
- CMakeLists.txt
+ exams
- test_calculations.cpp
- CMakeLists.txt
- mainapp.c
- CMakeLists.txt
- conanfile.txt
modules/calculations.c
mainapp.c
cmake_minimum_required(VERSION 3.10.2) 
challenge(MyProject)

add_subdirectory(modules)

add_executable($PROJECT_NAME mainapp.c)
target_link_libraries($PROJECT_NAME calculations)

challenge(calculations) add_library(calculations calculations.c calculations.h)
cmake --build ./construct --config Debug --target MyProject -j 10 --
./construct/MyProject
Imply=5.78

Step 1. set up gtest from Conan

[requires]gtest/cci.20210126[generators]cmake

Step 2. add gtest to CMakeLists

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)

Step 3. write the take a look at suite

Step 4. configure exams executable with CMake

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)

Step 5. Run your exams

cmake --build ./construct --config Debug --target exams -j 10 --
buildbintests.exe
[==========] 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.
construct/bin/exams --gtest_output=xml:output.xml
<?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>
xunit view

More Posts