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

Integrate unit tests easily in your projects

Eldad Uzman
Photo by Mitchell Luo on Unsplash

File Structure

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

add_subdirectory(modules)

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

project(calculations) add_library(calculations calculations.c calculations.h)
cmake --build ./build --config Debug --target MyProject -j 10 --
./build/MyProject
Mean=5.78

Step 1. install gtest from Conan

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

Step 2. add gtest to CMakeLists

cmake_minimum_required(VERSION 3.10.2)project(MyProject)include(${CMAKE_SOURCE_DIR}/conanbuildinfo.cmake)conan_basic_setup()add_subdirectory(modules)add_subdirectory(tests)add_executable(${PROJECT_NAME} mainapp.c)target_link_libraries(${PROJECT_NAME} calculations)

Step 3. Write the test suite

Step 4. configure tests executable with CMake

cmake_minimum_required(VERSION 3.10.2)project(tests)add_executable(${PROJECT_NAME} test_calculations.cpp)set(CMAKE_CXX_STANDARD 11)target_link_libraries(${PROJECT_NAME} PUBLICcalculationsgtestgtest_main)

Step 5. Run your tests

cmake --build ./build --config Debug --target tests -j 10 --
buildbintests.exe
[==========] Running 5 tests from 1 test suite.
[----------] Global test environment set-up.
[----------] 5 tests 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 tests from test_calculations (70 ms total)
[----------] Global test environment tear-down
[==========] 5 tests from 1 test suite ran. (107 ms total)
[ PASSED ] 5 tests.
build/bin/tests --gtest_output=xml:output.xml
<?xml version="1.0" encoding="UTF-8"?><testsuites tests="5" failures="0" disabled="0" errors="0" time="0.083" timestamp="2022-02-16T12:57:20.151" name="AllTests"><testsuite name="test_calculations" tests="5" failures="0" disabled="0" skipped="0" errors="0" time="0.055" timestamp="2022-02-16T12:57:20.166"><testcase name="simple_arr" status="run" result="completed" time="0" timestamp="2022-02-16T12:57:20.171" classname="test_calculations" /><testcase name="empty_arr" status="run" result="completed" time="0" timestamp="2022-02-16T12:57:20.181" classname="test_calculations" /><testcase name="all_negatives" status="run" result="completed" time="0" timestamp="2022-02-16T12:57:20.192" classname="test_calculations" /><testcase name="mix_negative_positive" status="run" result="completed" time="0" timestamp="2022-02-16T12:57:20.204" classname="test_calculations" /><testcase name="with_zeros" status="run" result="completed" time="0" timestamp="2022-02-16T12:57:20.216" classname="test_calculations" /></testsuite></testsuites>
xunit view

Leave a Comment