简体   繁体   English

GoogleTest测试装置说明

[英]GoogleTest Test Fixture Clarification

When I write Test fixture to test some c code I use the same set up from: https://github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-same-data-configuration-for-multiple-tests . 当我编写测试夹具来测试某些C代码时,我使用的是以下相同的设置: https : //github.com/google/googletest/blob/master/googletest/docs/primer.md#test-fixtures-using-the-多次测试相同的数据配置 The c code to be tested is as such: 要测试的c代码是这样的:

static struct {
    uint32_t success;
    uint32_t errors;
}stats;

uint32_t get_errors(void)
{
    return stats.errors;
}
uint32_t get_success(void)
{
    return stats.success;
}

void increment_errors(void)
{
    stats.errors++;
}
void increment_success(void)
{
    stats.success++;
}

void main_function(int arg)
{
    if (arg >=0)
        increment_success();
    else
        increment_errors();
}

Now when I write unit tests for this as such: 现在,当我这样编写单元测试时:

class MyTest : public ::testing::Test
{
protected:

    void SetUp(void)
    {
    }

    void TearDown(void)
    {
    }
};

TEST_F(MyTest, Test1)
{
        main_function(1);
    EXPECT_EQ(1, decoder_get_success());
    EXPECT_EQ(0, decoder_get_errors());
}

TEST_F(MyTest, Test2)
{
        main_function(40);
    EXPECT_EQ(1, decoder_get_success()); //This is a fail as number ends up being 2 not 1 which includes prev. test results
    EXPECT_EQ(0, decoder_get_errors());
}

Now I have noticed that when I write different test fixtures for this code the values of the stats struct variables do not reset ie if first test is supposed to increment success number then when we start the second test the number of success = 1 not 0 and so on and so forth. 现在我注意到,当我为该代码编写不同的测试夹具时,不会重置stats结构变量的值,即,如果第一个测试应该增加成功次数,那么当我们开始第二个测试时,成功次数= 1而不是0,并且等等等等。 I find this behavior odd as I assumed it is supposed to reset everything with each test. 我发现此行为很奇怪,因为我认为应该在每次测试时重置所有内容。

To solve this issue I ended up adding the following function to c code: 为了解决这个问题,我最终在c代码中添加了以下功能:

void stats_init(void)
{
    decoder_stats.errors = 0;
    decoder_stats.success = 0;
}

and add this to the TearDown(): 并将其添加到TearDown()中:

void TearDown(void)
{
    stats_init();
}

This makes sure that all gets reset. 这样可以确保全部重置。 The question here is this the correct behavior for gtests when using test fixtures? 这里的问题是使用测试治具时gtest的正确行为吗? Am I wrong to assume that it should not require m to define the init() function and add it to TearDown()? 我以为它不需要m来定义init()函数并将其添加到TearDown()中,这是我的错误吗?

The correct behavior of gtest is to create a new and fresh MyTest instance for each TEST_F you defined. gtest的正确行为是为您定义的每个TEST_F创建一个新的MyTest实例。

So, by defining a member attribute in the test fixture you are sure that you will have a different instance of your member attribute in each TEST_F 因此,通过在测试装置中定义成员属性,可以确保每个TEST_F中的成员属性都具有不同的实例

Unfortunetly, you are testing a static variable which is instanciate once. 不幸的是,您正在测试一次实例化的静态变量。 gtest does not magically known about it. gtest对此并不了解。 So, yes, you have to reset the value of your static structure between each TEST_F. 因此,是的,您必须在每个TEST_F之间重置静态结构的值。

Personnally, I will use SetUp() instead of TearDown to call stats_init. 就个人而言,我将使用SetUp()而不是TearDown来调用stats_init。

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

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