I think the main missing point in the other answer is that you need to #include
the test files.
Here is my way to split the tests:
- Put the tests in .h files potentially with header guards, although not needed if you take care.
- Have one main program as defined below that includes the test headers
- A Makefile that compiles + links the main test program.
Do not use the same name for a test twice across all files!
// main_test.cc
#include <gtest/gtest.h>
#include "test_a.h"
#include "test_b.h"
#include "test_c.h"
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
Use the Makefile from googletest and add the rules:
# compiles main test program
main_test.o : main_test.cc test_a.h test_b.h test_c.h $(GTEST_HEADERS)
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $< -o $@
# links test program
main_test : main_test.o
$(CXX) $(LDFLAGS) -L$(GTEST_LIB_DIR) $^ -lgtest_main -lpthread -o $@
I use a naming convention to order the tests by alphabetic letters:
// test_a.h
#include "some_class.h"
TEST(SomeClass, aName)
{
library::SomeClass a("v", {5,4});
EXPECT_EQ(a.name(), "v");
}
TEST(SomeClass, bSize)
{
library::SomeClass a("v", {5,4});
EXPECT_EQ(a.size(0), 5);
EXPECT_EQ(a.size(1), 4);
}
Then you can run individual tests with
./main_test --gtest_filter=SomeClass.a*
.cpp
files, go to your google-test powered C++ target in Xcode and activatePerform Single-Object prelink
in the build options. – Brufsky