如何在 Google 测试中 运行 一个夹具的多个测试用例?
How to run several test cases for one fixture in Google Test?
假设我有一个名为 ProfileTest
的 Google 测试夹具继承自 ::testing::TestWithParams<T>
,它创建了一个解析器:
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;
};
参数化测试用例:
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);
}
我添加了 TEST_CASE
格式正确的内容,一切都很好。
现在我想添加 TEST_CASE
格式错误的内容,但是 TestProfileGoodFormedContent
TEST_P
不适合测试错误内容。
我想我应该添加一个新的 TEST_P
,但它将具有相同的 fixture(ProfileTest)
,这给我带来了一个错误,即所有测试用例都将提供给具有 ProfileTest
作为夹具。
如何同时测试格式正确的内容和格式错误的内容?
在您的情况下,您需要与不同场景一样多的 Google 测试装置。
当然,您可以拥有基本装置 class - 它将为您设置常见的东西:
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;
};
所以 - 基本上您当前的 class 将成为基础 class。
对于 Good/Bad/Whatever-else - 创建特定夹具 classes:
class GoodProfileTest : public ProfileTestBase {};
class BadProfileTest : public ProfileTestBase {};
您当前的 "good" 个配置文件测试属于 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);
}
无论您需要作为不良配置文件进行测试 - 使用 BadProfileTest class。等等......当然 - 你需要为每个灯具使用 INSTANTIATE_*** 宏。
假设我有一个名为 ProfileTest
的 Google 测试夹具继承自 ::testing::TestWithParams<T>
,它创建了一个解析器:
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;
};
参数化测试用例:
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);
}
我添加了 TEST_CASE
格式正确的内容,一切都很好。
现在我想添加 TEST_CASE
格式错误的内容,但是 TestProfileGoodFormedContent
TEST_P
不适合测试错误内容。
我想我应该添加一个新的 TEST_P
,但它将具有相同的 fixture(ProfileTest)
,这给我带来了一个错误,即所有测试用例都将提供给具有 ProfileTest
作为夹具。
如何同时测试格式正确的内容和格式错误的内容?
在您的情况下,您需要与不同场景一样多的 Google 测试装置。
当然,您可以拥有基本装置 class - 它将为您设置常见的东西:
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;
};
所以 - 基本上您当前的 class 将成为基础 class。
对于 Good/Bad/Whatever-else - 创建特定夹具 classes:
class GoodProfileTest : public ProfileTestBase {};
class BadProfileTest : public ProfileTestBase {};
您当前的 "good" 个配置文件测试属于 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);
}
无论您需要作为不良配置文件进行测试 - 使用 BadProfileTest class。等等......当然 - 你需要为每个灯具使用 INSTANTIATE_*** 宏。