One of the many features provided by GTest is the ability to generate test at runtime. One useful application of this is that you can execute data-driven testing by obtaining a list of test file names, then generating a test for each of them. It’s actually very simple to do this in GTest – they call these value-parameterized tests (see documentation). Here’s a snippet of code to demonstrate how little overhead is involved:
#include <gtest/gtest.h> using namespace testing; // placeholder - could be used for test setup/cleanup class MyTest : public ::testing::TestWithParam<std::string> { }; TEST_P( MyTest, IntegrationTest ) { std::string test_file_name = GetParam(); // open file and run the integration test } std::vector<std::string> test_file_names(); INSTANTIATE_TEST_CASE_P( MyLibraryName, MyTest, test::ValuesIn( test_file_names() ) );
On one hand, this approach isn’t really in the spirit of unit test – data-driven tests have a habit of quickly escalating to integration tests between libraries. However, such integration tests also have their place in a testing strategy, and this is a neat way to accomplish it with minimal overhead.
Pingback: Unit Testing with GTest and GMock | musingstudio