It definitely sounds like a problem involving a shared resource (as you suspect).
Initialization of each test is done completely from within your test file, without any extra involvement from CMake, etc. As you mentioned, tests usually consist of an initialization phase followed by the actual test. Additionally, it's not unusual to include a cleanup step that takes place after the test completes. For example:
TEST(qsdf, asdf)
{
// ... initialization
// ... test
// ... **cleanup**
}
Typically, the initialization and cleanup phases just involve allocating and deallocating variables used in your test. But those phases are generally about making preparations to run your test, and cleaning up after it.
If you are testing code that shares resources, then the initialization phase consists of initializing those resources as needed; the cleanup phase consists of resetting those resources back to the state they were in before the initialization phase.
When you have tests that share resources, it's helpful to use a test fixture to coordinate the initialization/cleanup of the resources. This is the difference between the TEST
and TEST_F
macros in googletest - the latter represents a test that is based on a test fixture.
The googletest AdvancedGuide has a lot of information about using test fixtures:
The reason your code broke when you changed TEST
to TEST_F
is because you didn't define the test fixture that the test was expecting to find, which is the first parameter to the macro. In googletest, a test fixture is just a class that inherits from the ::testing::Test
class, so you would need to do something like this:
#pragma once
#include <gtest/gtest.h>
#include "My.hpp"
class MyTestFixture : public ::testing::Test
{
public:
virtual void SetUp() // This is defined in ::gtest::Test and overridden here
{
// This code will run before every test that uses this fixture
}
virtual void TearDown() // This is defined in ::gtest::Test and overridden here
{
// This code will run after every test that uses this fixture
}
};
/** Testing
*/
TEST_F(MyTestFixture, asdf)
{
// ... initialization (after SetUp)
// ... test
// ... cleanup (before TearDown)
}