tty*_*ty6 4 c++ testing qt unit-testing googletest
假设我有一个名为ProfileTest继承的 Google Test 固定装置,::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;
};
Run Code Online (Sandbox Code Playgroud)
参数化测试用例:
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);
}
Run Code Online (Sandbox Code Playgroud)
我添加TEST_CASE了格式良好的内容,任何东西都很好用。
现在我想添加TEST_CASE格式错误的内容,但TestProfileGoodFormedContent TEST_P不适合测试不良内容。
我想我应该添加一个 new TEST_P,但它会fixture(ProfileTest)带来一个错误,即所有测试用例都将提供给任何TEST_P具有ProfileTest作为夹具的测试用例。
我应该怎么做才能同时测试格式正确的内容和格式错误的内容?
在您的情况下,您需要与不同场景一样多的 Google 测试装置。
当然,你可以有基本的fixture类——它会为你设置一些常见的东西:
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;
};
Run Code Online (Sandbox Code Playgroud)
所以 - 基本上你当前的类将成为基类。
对于好/坏/其他 - 创建特定的夹具类:
class GoodProfileTest : public ProfileTestBase {};
class BadProfileTest : public ProfileTestBase {};
Run Code Online (Sandbox Code Playgroud)
您当前的“良好”配置文件测试属于 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);
}
Run Code Online (Sandbox Code Playgroud)
无论您需要作为不良配置文件进行测试 - 使用 BadProfileTest 类。依此类推...当然 - 您需要为每个装置使用 INSANTIATE_*** 宏。