I have a test fixture that shares most of the test code. The variations are mostly from a parameter value. I would like to create SetUp() / TearDown() for them, but they (obviously) can't be called with a parameter.
What would the best strategy to use SetUp() / TearDown() mechanisms while avoiding code duplication?
This is my original code :
class FileToolsTest : public testing::Test
{
protected:
void TestReadAndWrite(const wstring& filename, const wstring& content)
{
FileTools::RemoveFile(filename);
// Test code there
ok = FileTools::RemoveFile(filename);
ASSERT_EQ(true, ok);
}
};
//-------------------------------------------------------------------------
TEST_F(FileToolsTest, ReadAndWrite_case1)
{
const wstring testFilename = L"testfile";
const wstring testContent = L"This\nis\ndummy\ndata";
TestReadAndWrite(testFilename, testContent);
}
TEST_F(FileToolsTest, ReadAndWrite_case2)
{
const wstring testFilename = L"testfíle";
const wstring testContent = L"This\nis\ndummy\n\nuníc@de datã éééé";
TestReadAndWrite(testFilename, testContent);
}
TEST_F(FileToolsTest, Write_case3)
{
const wstring testFilename = L"Micka\x00ebl/fileWithUnicodeFolderInPath2.txt";
if (FileTools::FileExists(testFilename))
{
const bool ok = FileTools::RemoveFile(testFilename);
ASSERT_TRUE(ok);
}
// Test code there
FileTools::RemoveFile(testFilename);
}
As you can see, ::RemoveFile() is duplicated between TestReadAndWrite() and Write_case3(), both at test start and test end.