Install Google Test
- Install cmake
- Get the last version of google test from https://github.com/google/googletest/releases and unzip it
- Go to the google test folder
- Run:
cmake .
sudo cmake --build . --target install
Build the tests
- Create a build folder and navigate to it
- Run: ``` cmake PATH/TO/test cmake –build . ``` In the build folder, the test executable will be created.
Run the tests
The test executable can be run individualy or by calling ctest
How to add Tests
Create test source code
In a Cpp file, create a test fixture class. For example, see the following code (taken from https://www.linuxembedded.fr/2014/09/introduction-a-google-c-testing-framework/)
class FooTest : public ::testing::Test
{
protected:
FooTest()
{
}
virtual ~FooTest()
{
}
virtual void SetUp()
{
}
virtual void TearDown()
{
}
};
TEST_F(FooTest, MethodBarDoesAbc)
{
const string input_filepath = "this/package/testdata/myinputfile.dat";
const string output_filepath = "this/package/testdata/myoutputfile.dat";
Foo f;
EXPECT_EQ(0, f.Bar(input_filepath, output_filepath));
}
TEST_F(PressionTest, testConvertSensor2PressureCompWithFloat)
Test convertSensor2Pressure with comparison with old floating point function.
Add the test source file to CMakeLists.txt
We need to create a new executable for the new test fixture. Add the following code to the CMakeList.txt
set(NEW_TEST_SRC test_foo.cpp
# Other needed source files
)
add_executable(test_foo ${NEW_TEST_SRC})
target_link_libraries(test_foo GTest::GTest GTest::Main)
add_test(TestFoo test_foo) # add the test to the registry to be run with ctest