...
Code Block | ||
---|---|---|
| ||
mkdir build cd build cmake .. make runUnitTests.o ./runUnitTests.o |
5.1 Errors and fixes
Cannot find googletest
Uninitialized git submodules
This problem has many different manifestations. They include
cannot find MadgwickAHRS.h
cannot find googletest
A common error when compiling unit tests will be something in terms of cannot find googletest
. That probably means you haven't initialized git submodules, so run git submodule update --init
Error: leaked mock object
As this error tells you, you have a leaked mock object, which was not deleted. There are multiple possible reasons for this:
...
- that destructor is called (you are deleting the component under test in the test)
- that destructor is virtual (you might be deleting the component but it's its destructor isn't virtual so the child one isn't being called)
- you are deleting the dependency in that destructor.
...
If the mocked object is not a direct dependency of the component under test, it is not responsible for deleting it, so you should delete the mock yourself.
4. You used a mock after it has been deleted
Because of how GoogleTest operates under the hood, setting expectations on a deleted mock will not trigger any segfaults, however, GoogleTest will think that the mock wasn't deleted.
Code Block | ||||
---|---|---|---|---|
| ||||
TEST_F(Fixture1, Test1) {
Mock * ourMock = new Mock; // The mock is created (could also be in the test fixture constructor) at address 0x5634d2e8ca736700
EXPECT_CALL(*ourMock, fun()); // We set expectations on the mock
code->run1(); // We hope this calls ourMock->fun();
delete ourMock; // Here, expectations on ourMock are verified and cleanup is performed. GoogleTest is happy.
EXPECT_CALL(*ourMock, fun()); // OH NO! GoogleTest now again thinks 0x5634d2e8ca736700 is still active
} // after all cleanup, GoogleTest will report "Mock leaked at 0x5634d2e8ca736700 used in Fixture1.Test1", despite it was deleted on line 8.
|
Make sure you don't set EXPECT_CALLs after you delete the mock. Sometimes, they can hide in the test destructor, or in a mock destructor.
SIGSEGV: Segmentation fault
...