GoogleTest - Running First Test



In this tutorial, we will learn how to use GoogleTest for testing C++ code with the help of an example. We will go through all the required steps, commands and methods that are used while running a basic test.

running test on googletest

Creating a Basic Test

Follow the steps given below to create a basic test in GoogleTest for a C++ function −

Step 1:

First, create a folder or directory where you want to save your project. Type the following command on your system's command prompt to create a folder −

mkdir test1

Here, we are using name of the folder as "test1".

Step 2:

Create a "test_case.cc" file which will contain a C++ function and unit tests for that function. You can give any name of your choice.

// header of the GoogleTest
#include <gtest/gtest.h>

// function for which we write unit tests
int add(int num1, int num2) {
    return num1 + num2;
}

// to test addition of positive numbers
TEST(SumTest, ForPositiveNumbers) {
    EXPECT_EQ(35, add(23, 12));
}

// to test addition of negative numbers
TEST(SumTest, ForNegativeNumbers) {
    EXPECT_EQ(-1, add(-1, 0));
}

// to test addition of positive and negative numbers
TEST(SumTest, ForMixedNumbers) {
    EXPECT_EQ(0, add(-1, 1));
}

// main() function to run all tests
int main(int argc, char **argv) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

In the above code, the first unit test checks if the given C++ function is able to handle two positive numbers properly or not. The second test checks if the add() function correctly adds a negative number and zero. The last unit test checks whether the add() function correctly adds a negative and a positive numbers together. Lastly, the main() function initializes Google Test and runs all the test cases.

Create "CMakeLists.txt" File

As discussed in previous chapter, your each GoogleTest project requires "CMakeLists.txt" file. In this file, we declare the dependency on GoogleTest by using its github link as shown below −

cmake_minimum_required(VERSION 3.14)
project(test1)

# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

include(FetchContent)
FetchContent_Declare(
  googletest
  URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# to enable test
enable_testing()

# defining executable target and its source file
add_executable(
  test_case
  test_case.cc
)
# to link the executable with GoogleTest main library
target_link_libraries(
  test_case
  GTest::gtest_main
)

# to include GoogleTest module
include(GoogleTest)

# to discover and register all tests in the executable
gtest_discover_tests(test_case)

Build & Run the Tests

To build and run your test, follow the steps given below −

Step 1:

Navigate to the project folder using the following command −

cd test1

Step 2:

Then, use the below command to generate a build file −

cmake -S . -B build

-S specifies the source directory, which is "." and -B specifies the build directory, which is named "build".

Step 3:

Now, compile and build the project with the help of build file generated by CMake in the previous step −

cmake --build build

Step 4:

Move to the build directory −

cd build

Step 5:

Lastly, run all tests using the given command −

ctest

The above mentioned command runs the tests that have been defined in the "CMakeLists.txt" file using the enable_testing() commands.

Output:

The output would appear like this on cosole −

Test project D:/gTest/test1/build
    Start 1: SumTest.ForPositiveNumbers
1/3 Test #1: SumTest.ForPositiveNumbers .......   Passed    0.02 sec
    Start 2: SumTest.ForNegativeNumbers
2/3 Test #2: SumTest.ForNegativeNumbers .......   Passed    0.01 sec
    Start 3: SumTest.ForMixedNumbers
3/3 Test #3: SumTest.ForMixedNumbers ..........   Passed    0.01 sec

100% tests passed, 0 tests failed out of 3

Total Test time (real) =   0.13 sec

You can check the result of each test in the LastTest.log file. This file is available at the location: /build/Testing/Temporary.

Advertisements