Writing and Running Test Cases in C++


Unit testing is an essential part of software development, as it ensures that individual units of code work as expected. In C++, there are several testing frameworks available for writing and running test cases. In this article, we will explore how to write and run test cases using popular testing frameworks like Google Test and Catch2.

What Are Test Cases?

A test case is a set of conditions or inputs used to check whether a software application behaves as expected. It involves writing code that tests a specific function or feature in isolation. When the function behaves correctly under all the defined conditions, the test passes; otherwise, it fails.

Using Google Test for Writing Test Cases

Google Test is one of the most widely used frameworks for writing unit tests in C++. It provides powerful assertions and is easy to integrate with existing projects. Below are the steps to set up and write test cases using Google Test.

Step 1: Install Google Test

You can install Google Test manually or using package managers like vcpkg or conan. Below are the steps to install it using vcpkg:

    vcpkg install gtest
        

Step 2: Write Your First Test Case with Google Test

Once Google Test is installed, you can write your test cases. Let’s assume we have a simple function that adds two integers:

Function to Test:

    // math_functions.h
    #ifndef MATH_FUNCTIONS_H
    #define MATH_FUNCTIONS_H

    int add(int a, int b);

    #endif
        
    // math_functions.cpp
    #include "math_functions.h"

    int add(int a, int b) {
        return a + b;
    }
        

Test Case for the Add Function:

    // test_math_functions.cpp
    #include 
    #include "math_functions.h"

    // Test case for add function
    TEST(AdditionTest, HandlesPositiveInput) {
        EXPECT_EQ(add(1, 2), 3);  // Check if 1 + 2 equals 3
    }

    TEST(AdditionTest, HandlesNegativeInput) {
        EXPECT_EQ(add(-1, -2), -3);  // Check if -1 + (-2) equals -3
    }

    TEST(AdditionTest, HandlesZeroInput) {
        EXPECT_EQ(add(0, 0), 0);  // Check if 0 + 0 equals 0
    }
        

Step 3: Compile and Run the Tests

To compile and run the test cases, use the following commands:

    g++ test_math_functions.cpp -lgtest -lgtest_main -pthread -o test_app
    ./test_app
        

If the tests pass, the output will indicate success. Otherwise, it will show which test case failed.

Using Catch2 for Writing Test Cases

Catch2 is another popular and easy-to-use unit testing framework for C++. It is header-only, which makes it simple to set up and use. Below are the steps to write and run test cases using Catch2.

Step 1: Install Catch2

Catch2 is header-only, so it is very easy to integrate into your project. You can download the header file from the Catch2 GitHub repository:

git clone --branch v2.x https://github.com/catchorg/Catch2.git

Then, copy the single_include/catch2 directory to your project directory.

Step 2: Write Your First Test Case with Catch2

Now that Catch2 is installed, you can write test cases in a very simple way. Let’s test the same add function using Catch2:

Function to Test:

    // math_functions.h
    #ifndef MATH_FUNCTIONS_H
    #define MATH_FUNCTIONS_H

    int add(int a, int b);

    #endif
        
    // math_functions.cpp
    #include "math_functions.h"

    int add(int a, int b) {
        return a + b;
    }
        

Test Case for the Add Function:

    // test_math_functions.cpp
    #define CATCH_CONFIG_MAIN
    #include 
    #include "math_functions.h"

    TEST_CASE("Add function", "[add]") {
        SECTION("Handles positive input") {
            REQUIRE(add(1, 2) == 3);
        }

        SECTION("Handles negative input") {
            REQUIRE(add(-1, -2) == -3);
        }

        SECTION("Handles zero input") {
            REQUIRE(add(0, 0) == 0);
            REQUIRE(add(1, 0) == 1);
        }
    }
        

Step 3: Compile and Run the Tests

To compile and run the tests, use the following commands:

    g++ -std=c++11 test_math_functions.cpp -o test_app
    ./test_app
        

If all tests pass, you will see a message indicating that all tests were successful. If any test fails, Catch2 will provide detailed output about which test failed.

Test Assertions in Google Test and Catch2

Both Google Test and Catch2 offer various assertions that help in writing test cases. These assertions check whether the expected result matches the actual output from the function being tested.

Assertions in Google Test:

  • EXPECT_EQ(val1, val2) – Checks if val1 is equal to val2.
  • EXPECT_NE(val1, val2) – Checks if val1 is not equal to val2.
  • EXPECT_TRUE(condition) – Checks if the condition is true.
  • EXPECT_FALSE(condition) – Checks if the condition is false.

Assertions in Catch2:

  • REQUIRE(expression) – Ensures that the expression evaluates to true. The test will stop if the expression fails.
  • CHECK(expression) – Checks that the expression evaluates to true. The test continues even if the expression fails.

Conclusion

Writing and running test cases in C++ is a vital part of the development process. Using frameworks like Google Test and Catch2 makes it easier to write, organize, and run tests for your code. Both frameworks provide rich sets of features for verifying the correctness of functions, and they help developers identify bugs early in the development cycle. By integrating unit testing into your workflow, you can ensure the reliability and maintainability of your C++ projects.





Advertisement