简体   繁体   English

如何在Google Test中为一个灯具运行多个测试用例?

[英]How to run several test cases for one fixture in Google Test?

Suppose that I have a Google Test fixture named ProfileTest inherited from ::testing::TestWithParams<T> that creates a parser: 假设我有一个名为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;
};

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. 我添加了TEST_CASE其中包含格式良好的内容,任何功能都很棒。

Now I want to add TEST_CASE with bad-formed content, but TestProfileGoodFormedContent TEST_P is unsuitable for testing bad content. 现在我想添加TestProfileGoodFormedContent错误的TEST_CASE ,但TestProfileGoodFormedContent TEST_P不适合测试不良内容。

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. 我想我应该添加一个新的TEST_P ,但它会有相同的fixture(ProfileTest) ,它会给我一个错误,所有的测试用例都会提供给任何有ProfileTest作为fixture的TEST_P

What should I do to test well-formed content and bad-formed content simultaneously? 我该怎么做才能同时测试格式良好的内容和格式错误的内容?

In your case, you need as many Google Test fixtures as you have different scenarios. 在您的情况下,您需要尽可能多的Google测试夹具,因为您有不同的场景。

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: 对于Good / Bad / Whatever-else - 创建特定的夹具类:

class GoodProfileTest : public ProfileTestBase {};
class BadProfileTest : public ProfileTestBase {};

You current "good" profile test would belongs to GoodProfileTest: 您当前的“好”配置文件测试属于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. 无论你需要测试什么是错误的配置文件 - 使用BadProfileTest类。 And so on... Of course - you need to use INSTANTIATE_*** macros for every fixtures. 等等......当然 - 你需要为每个灯具使用INSTANTIATE _ ***宏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM