This tutorial provides an overview of unit testing methods and discusses the four testing frameworks supported by CLion: Google Test, Boost.Test, Catch2, and Doctest.
Unit Testing Basics
Unit tests are designed to check individual units of source code separately. Here, a "unit" refers to the smallest part of code that can be tested independently, such as free functions or class methods.
Unit testing helps to:
<strong>Modularize</strong> your code
Since the testability of code depends on its design, unit tests help break it down into specialized, easy-to-test parts.
<strong>Avoid Regression Bugs</strong>
When you have a set of unit tests, you can iteratively run them to ensure that everything works correctly each time you add new features or introduce changes.
<strong>Document</strong> your code
Running, debugging, or even just reading tests can provide a wealth of information about how the original code works, allowing you to use them as implicit documentation.
π Click to receive π
π C Language Knowledge Resource Collection
A single unit test is a method for checking a specific functionality with clear pass/fail criteria. The general structure of a single test is as follows:
Test (TestGroupName, TestName) { 1 - setup block 2 - running the under-test functionality 3 - checking the results (assertions block)}
Good practices for unit testing include:
-
Create tests for all public functions, including class constructors and operators.
-
Cover all code paths and check all trivial and edge cases, including cases with incorrect input data (refer to negative testing).
-
Ensure that each test works independently and does not block the execution of other tests.
-
Organize tests in a way that does not affect the results based on the order of execution.
It is useful to group test cases when the tests are logically related or use the same data. Suites combine tests with common functionality (for example, different cases executing the same function).
Fixture classes help organize shared resources for multiple tests. They are used to set up and clean up the environment for each test in the group, avoiding code duplication.
Unit tests are often used in conjunction with mocks. Mock objects are lightweight implementations of the test targets used when the functionality being tested contains complex dependencies and it’s difficult to construct viable test cases using real objects.
Frameworks
Manual unit testing involves many routine operations: writing stub test code, implementing main(), printing output messages, etc. Unit testing frameworks not only help automate these operations but also provide benefits such as:
-
Manageable assertion behavior
With a framework, you can specify whether a single check failure should cancel the entire test execution: in addition to regular ASSERTs, frameworks also provide EXPECT/CHECK macros that do not interrupt the test program on failure.
-
Various checkers
Checkers are macros used to compare expected and actual results. The checkers provided by testing frameworks often have configurable severity (warnings, general expectations, or requirements). Additionally, they may include tolerances for floating-point comparisons and even pre-implemented exception handlers to check for exceptions being thrown under certain conditions.
-
Test organization
Using a framework makes it easy to create and run subsets of tests grouped by common functionality (suites) or shared data (fixtures). Moreover, modern frameworks automatically register new tests, so you donβt have to register them manually.
-
Customizable messages
Frameworks handle test output: they can display detailed descriptive output, user-defined messages, or just brief pass/fail results (the latter is especially convenient for regression testing).
-
XML reporting
Most testing frameworks offer the ability to export results in XML format. This is very useful when you need to pass results further to continuous integration systems (like TeamCity or Jenkins).
Unit Testing in CLion
CLion’s integration with Google Test, Boost.Test, Catch2, and Doctest includes:
-
Complete code insight into the framework libraries,
-
Dedicated run/debug configurations,
-
Gutter icons to run or debug tests/suites/fixtures and check their status,
-
Dedicated test runners,
-
And code generation for tests and fixture classes (for Google Tests).
For CMake projects, CLion also supports CTest.
Setting Up a Testing Framework for Your Project
In this section, we will discuss how to add the Google Test, Boost.Test, Catch2, and Doctest frameworks to a CLion project, and how to write a simple set of tests.
As an example, we will use the DateConverter project, which you can clone from the GitHub repository.
This program calculates the absolute value of a date given in Gregorian format and converts it to Julian date. Initially, the project contains no tests – we will gradually add tests.
To see the differences between the frameworks, we will execute the same tests using all four frameworks.
You can find the final version of the project in the DateConverter_withTests repository. Below is an example of how the project structure will change:
Google Test Framework
Google Test is one of the popular testing frameworks provided by Google. It can be used together with the Google Mock library, which provides a convenient way to create and use mock objects.
To add Google Test to your project, follow these steps:
-
First, add the necessary Google Test header files and source files to the project. These files are located in the /googletest/include/ and /googletest/src/ directories of the Google Test repository.
-
In the CMakeLists.txt file, add the following lines to include the Google Test framework and test code:
# Set CMake to search for the Google Test library include(FetchContent) FetchContent_Declare(googletest GIT_REPOSITORY https://github.com/google/googletest.git GIT_TAG release-1.10.0) FetchContent_MakeAvailable(googletest) # Add test code to the project enable_testing() add_executable(Unit_Tests tests/test1.cpp tests/test2.cpp) target_link_libraries(Unit_Tests gtest_main) # Run tests include(GoogleTest) gtest_discover_tests(Unit_Tests)
-
Create test files (e.g., test1.cpp and test2.cpp) in the tests/ directory. These files should contain the test code.
-
Use CLion’s testing tools to run and debug the tests.
Adding Google Test to your project is not difficult, but it does require some steps. Make sure you are familiar with these steps and remember how to configure Google Test tests.
Recommended Reads
-
Inappropriate people, treating squeezing employees as a badge of honor…
-
What majors do you not recommend for younger students? Netizens: Computer Science…
-
Because of Musk, the original tribe is addicted to yellow and cannot extricate themselves.