C++ Unit Tests — 1

  • Arrange : Preparations for the unit test ( initializing variables,dependencies)
  • Act : Running the unit test
  • Assert : verifies the Unit test’s outcome.
TEST(myTests, addTwoNumReturnsInt)
{
int result {0}; // Arrange
result = addTwoNum(10, 22); // Act GTEST_ASSERT_EQ(result, 32); // Assert
}
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
#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();
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sachith Muhandiram

Sachith Muhandiram

DevOps who is willing to learn and try new things.