With Google test I want to specify a Test fixture for use in different test cases.
The fixture shall allocate and deallocate objects of the class TheClass
and its data management class TheClassData
, where the data management class requires the name of a datafile.
For the different tests, the file name should vary.
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 the fixture with different file names. Imagine this as setting up a test environment.
The question: How can I specify the filename from a test, i.e. how to call a non-default constructor of a fixture?
I found things like ::testing::TestWithParam<T>
and TEST_P
, which doesn't help, as I don't want to run one test with different values, but different tests with one fixture.
TEST_F
) using the fixture. So the answer is, that it's not possible? Thanks. – ClementeclementiTestWithParam<T>
andTEST_P
is exactly what you need. Lookup the Advanced Docs how to use them in practice. You can always instantiate the instance under test (I assume it'sTheClass
) inside of the testcase. – Aedes