You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

This tutorial focuses on creating a simple C++ Calculator project using the divide() method and testing it using the GoogleTest platform, and a complete way to do it.

1. Create The Calculator project

1.1. SOURCES

To present an example of using GoogleTest, we first need to create a project in C ++. In the project directory named Calculator, we will create the C ++ source and header files in the appropriate directories. For the tutorial, we create operations.cpp in the src directory, which contains a simple divide function and the operation.h header file.

We also need to create a folder for our tests. In this tutorial, this folder is called tst. To start writing tests, we will create run_tests.cpp and test_operations.cpp. We want GoogleTest to search for test files automatically, and we do this thanks to the code stored in the run_tests.cpp source file. Typically, this code will be universal for all kinds of projects:

run_tests.cpp
#include "gtest/gtest.h"

int main(int argc, char **argv){
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}


However, the actual test cases are placed in a separate file, the name of which corresponds to the name of the file that stores the code to be tested. It is good practice to name the test file using the name of the source file being tested with the keyword test appended as a prefix or suffix separated by the underscore "_" or the dash "-". The goal of this tutorial is to test the divide function stored in the operations.cpp file. Therefore, the file with the test code is named test_operations.cpp.


Our project folder should look like this:

  • No labels