Reputation: 1233
Suppose that I have a Google Test fixture named ProfileTest
inherited from ::testing::TestWithParams<T>
that creates a parser:
class ProfileTest:
public ::testing::TestWithParam<std::tuple<std::string,std::string>>{
public:
QString getName(){
return QFileInfo(*m_file).fileName();
}
protected:
void SetUp(){
m_profile = new Profile();
m_file = new QFile(std::get<0>(GetParam()).c_str());
m_file->open(QIODevice::WriteOnly | QIODevice::Text);
m_file->write(std::get<1>(GetParam()).c_str());
m_file->close();
}
void TearDown(){
delete m_file;
delete m_profile;
}
Profile* m_profile;
QFile *m_file;
};
Parametrized test case:
TEST_P(ProfileTest, TestProfileGoodFormedContent){
ASSERT_NO_THROW(m_profile->readProfile(QFileInfo(*m_file)));
ASSERT_STREQ(m_profile->name(), getName());
ASSERT_GE(m_profile->getProfileConfigurations().size(),1);
}
I have added TEST_CASE
with well-formed content, and anything works great.
Now I want to add TEST_CASE
with bad-formed content, but TestProfileGoodFormedContent
TEST_P
is unsuitable for testing bad content.
I suppose I should add a new TEST_P
, but it will have same fixture(ProfileTest)
that brings me an error that all test cases will be provided to any TEST_P
that have ProfileTest
as fixture.
What should I do to test well-formed content and bad-formed content simultaneously?
Upvotes: 4
Views: 1657
Reputation: 24420
In your case, you need as many Google Test fixtures as you have different scenarios.
Of course, you can have base fixture class - that will sets up for you common things:
class ProfileTestBase :
public ::testing::TestWithParam<std::tuple<std::string,std::string>>{
public:
QString getName(){
return QFileInfo(*m_file).fileName();
}
protected:
void SetUp(){
m_profile = new Profile();
m_file = new QFile(std::get<0>(GetParam()).c_str());
m_file->open(QIODevice::WriteOnly | QIODevice::Text);
m_file->write(std::get<1>(GetParam()).c_str());
m_file->close();
}
void TearDown(){
delete m_file;
delete m_profile;
}
Profile* m_profile;
QFile *m_file;
};
So - basically your current class will become base class.
For Good/Bad/Whatever-else - create specific fixture classes:
class GoodProfileTest : public ProfileTestBase {};
class BadProfileTest : public ProfileTestBase {};
You current "good" profile test would belongs to GoodProfileTest:
TEST_P(GoodProfileTest, TestProfileGoodFormedContent){
ASSERT_NO_THROW(m_profile->readProfile(QFileInfo(*m_file)));
ASSERT_STREQ(m_profile->name(), getName());
ASSERT_GE(m_profile->getProfileConfigurations().size(),1);
}
Whatever you need to be tested as bad profile - use BadProfileTest class. And so on... Of course - you need to use INSTANTIATE_*** macros for every fixtures.
Upvotes: 5