Specify constructor arguments for a Google test fixture

In a Google test, I want to specify a test device for use in different test cases. The device must allocate and free objects of the TheClass class and its data management class TheClassData , where the data file class is required for the data management class.
For different tests, the file name should be different.

I defined the following Fixture:

 class TheClassTest : public ::testing::Test { protected: TheClassTest(std::string filename) : datafile(filename) {} virtual ~TheClassTest() {} virtual void SetUp() { data = new TheClassData(datafile); tc = new TheClass(data); } virtual void TearDown() { delete tc; delete data; } std::string datafile; TheClassData* data; TheClass* tc; }; 

Now different tests should use a device with different file names. Imagine that is setting up a test environment.

Question: How can I specify the file name from the test, for example, how to call the constructor of a non-standard device type?

I found things like ::testing::TestWithParam<T> and TEST_P , which does not help, because I do not want to run one test with different values, but different tests with one device.

+5
source share
2 answers

As suggested by another user, you cannot achieve what you want by creating an instance using a constructor other than the standard one. However, there are other ways. Just overload the SetUp function and call this version explicitly in the tests:

 class TheClassTest : public ::testing::Test { protected: TheClassTest() {} virtual ~TheClassTest() {} void SetUp(const std::string &filename) { data = new TheClassData(filename); tc = new TheClass(data); } virtual void TearDown() { delete tc; delete data; } TheClassData* data; TheClass* tc; }; 

Now in the test, just use this overload to configure the file name:

 TEST_F(TheClassTest, MyTestCaseName) { SetUp("my_filename_for_this_test_case"); ... } 

Without parameters, TearDown automatically cleared when the test is completed.

+3
source

Use the current class as the base class for your appliances:

 class TheClassTestBase : public ::testing::Test { protected: TheClassTestBase(std::string filename) : datafile(filename) {} ... }; 

For each specific file name, use a derived device:

 class TheClassTestForFooTxt : public TheClassTestBase { protected: TheClassTestForFooTxt() : TheClassTestBase ("foo.txt") {} }; 

However, this is an additional step necessary for each set of parameters, so you can try using templates or macros to do this with less effort. How:

 template <typename ClassTestTag> struct ClassTestParams { static std::string filename; }; template<typename ClassTestTag> class TheClassTest : public TheClassTestBase { protected: TheClassTest() : TheClassTestBase (ClassTestParams<ClassTestTag>::filename) {} }; 

Then - for each set of parameters - do the following:

 class FooTxtTag {}; template <> std::string ClassTestParams<FooTxtTag>::value = "foo.txt"; using TheClassTestForFooTxt = TheClassTest<FooTxtTag>; TEST_F(TheClassTestForFooTxt, xxxx) {} 

However - in your particular case - I would also try GoogleTest: type-parameterized-tests .

0
source

All Articles