C++ Unit Tests — 1
Recently I had been studying C++ and Unit Testing (C++ from this Udemy course and Unit Testing from this book). Finally I landed on this Test Driven Development in C++ course. From this articular onward I will share my experiences with these. In this post, I will describe what is a Unit test and how to add googletest to your project.
Unit Test
A unit test simply verifies the behavior of a small piece of code (simply a function). It does not verify your coding standards right, you validated inputs ,you used proper syntax etc. It simply a black-box test.
As an example, function is taking two integers and returns the sum of them. A unit test should verify only it returns right addition. It should not check whether passed numbers are integers.
A Unit test has three basic parts.
- Arrange : Preparations for the unit test ( initializing variables,dependencies)
- Act : Running the unit test
- Assert : verifies the Unit test’s outcome.
A simple googletest sample following this would be like :
TEST(myTests, addTwoNumReturnsInt)
{
int result {0}; // Arrange result = addTwoNum(10, 22); // Act GTEST_ASSERT_EQ(result, 32); // Assert
}
For simplicity I broke it to three pieces, it could simply be written as GTEST_ASSERT_EQ(addTwoNum(10, 22), 32);
.We will discuss about all these terms,naming things in googletests in next post.
addTwoNumReturnsInt
is the name of this unit test. Its a bit longer name. But it has a meaningful name which even non-developer can understand what this test does.
In unit test arrangement, we can have multiple arrange sections as we need to initialize some variables. But its important to remember.
act and assert should have one line for each section. Its sort of the best practice with unit tests. Specially with act section. If we have multiple act , that means we are trying to verify multiple behaviors in a unit test. Its simply an integration test.
Adding GoogleTest to C++
We have to install cmake
into our PC. For debian based OS
sudo apt install cmake
If you use VS-Code for development, add CMake extension. In your project folder , first have to create a CMakeLists.txt
file. This is used by CMake to build our project. In this file
cmake_minimum_required(VERSION 3.12) # Requiredproject(my_cpp_project) # Requiredadd_subdirectory(googletest) # googletest subdirectoryinclude_directories(googletest/include) add_executable(medium medium.cpp) # Requiredtarget_link_libraries(medium PRIVATE gtest) # Required for googletest
medium.cpp File would be like
#include <gtest/gtest.h>int addTwoNum(int x,int y){
return x+y;
}TEST(myTests, addTwoNumReturnsInt)
{
int result {0};
result = addTwoNum(10, 22);
GTEST_ASSERT_EQ(result, 32);
}int main(int argc, char* argv[]){ ::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();}
Then clone googletest project to your project working directory.
git clone https://github.com/google/googletest.git
After adding this, VS code will automatically ask for the environment to use. I used gcc 9.3.0 with C and C++
If things are working, you will see output similar to this
Here I tried to explain basis of Unit tests as I understood and configuring GoogleTest. Next post, I will try to explain more about cmake and googletests.
Stay Safe!!
Ref :