简体   繁体   English

是否有更好的解决方案来使用 Google 测试框架测试 getter

[英]Is there a better solution to test getters with the Google Test Framework

I am testing the getters of a class.我正在测试 class 的吸气剂。 Each getter has some regex code to validate the input.每个 getter 都有一些正则表达式代码来验证输入。

I use the Google Test Framework to write the unit tests.我使用 Google 测试框架来编写单元测试。

Every time I want to test a new getter I need to expand the parameters of the constructor.每次我想测试一个新的 getter 时,我都需要扩展构造函数的参数。 I need to update the code of the previous tests to not break the previous tests.我需要更新之前测试的代码,以免破坏之前的测试。

Like this:像这样:

TEST_F(wsRecordTest,DoesItThrowExceptionWhenWrongCitynameIsProvided)
{
    weatherdayRecord wsRecord{"!#Delft$*"}; --> has to be: weatherdayRecord wsRecord{"!#Delft$*","2020-10-03"}; 
    ASSERT_THROW(wsRecord.getCity(),std::invalid_argument);
}

// Test accessor getDate() and constructor
TEST_F(wsRecordTest,DoIGetTheRightDateFromTheConstructor)
{
    weatherdayRecord wsRecord{"Delft","2020-10-03"};
    ASSERT_EQ(wsRecord.getDate(),"2020-10-03");
}

Is there a way to avoid this?有没有办法避免这种情况?

How about adding a SetUp functionality to your fixture?为您的灯具添加设置功能怎么样?

class wsRecordTest : public ::testing::Test {
    protected:
        std::unique_ptr<weatherdayRecord> record;
        void SetUp() override {
            record = std::make_unique<weatherdayRecord>("Delft", "2020-10-03");
        }
}

TEST_F(wsRecotdTest, CheckDate) {
     ASSERT_EQ(record->getDate(), "2020-10-03");
}

Not sure though why you want to test your getters if the classes are just holders for data..如果类只是数据的持有者,不确定为什么要测试你的吸气剂。

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

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