I've pushed things about in the source repo, moving the code into a library directory, which means I can link it into the existing mains, as well as into new gtests. In the CMake system, I've conditioned building tests on the existence of the googletest project being available as a subdirectory. I use enough different compilers and build options that trying to use a system build of gtest just doesn't work. The best, and recommended, choice, is to build googletest as part of your project. That way any ABI impacting subtlety, like using a different C++ standard library, is take care of automatically. The bit of cmake magic is in the top level CMakeLists.txt :
if (EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/googletest/CMakeLists.txt")
add_subdirectory(googletest EXCLUDE_FROM_ALL)
add_subdirectory(tests)
else()
message("GTEST Not Found at ${CMAKE_CURRENT_SOURCE_DIR}/googletest/CMakeLists.txt")
endif()
This looks for googletest to be available, and if it is, add it to the project, and my tests subdirectory, otherwise issue a message. I prefer this to attempting to fix up the missing gtest automatically. That always seems to cause me problems, such as when I'm operating disconnected, on a train, like right now.
The tests I have are pretty simple, not much more than primitive breathing tests.
TEST_F(BatchTest, run1Test)
{
Batch batch;
batch.add([this](){incrementCalled();});
EXPECT_EQ(0u, called);
batch.run();
EXPECT_EQ(1u, called);
}
or, to make sure that passing arguments worked
TEST_F(BatchTest, runArgTest)
{
Batch batch;
int i = 0;
batch.add([&i](int k){ i = k;}, 1);
EXPECT_EQ(0, i);
batch.run();
EXPECT_EQ(1, i);
}
I don't actually expect to find runtime errors with these tests. They exercise ths component just enough that I'm not generating compile errors in expected use cases. Template code can be tricky that way. Templates that aren't instantiated can have horrible errors, but the compiler is willing to let them pass, if they mostly parse.
SFINAE may not be your friend.