简体   繁体   English

如何使用多组初始条件运行测试?

[英]How do I run a test with multiple sets of initial conditions?

I currently have a suite of tests which are are part of a test fixture.我目前有一套测试,它们是测试装置的一部分。 I want to run the same set of tests with a different test fixture too.我也想用不同的测试夹具运行相同的测试集。

How do I do this without actually having to copy-paste the tests and 'manually' changing the test fixture name (as shown below)?如何在不实际复制粘贴测试和“手动”更改测试夹具名称的情况下执行此操作(如下所示)?

class Trivial_Test : public ::testing::Test
{
    void SetUp()
    {
        ASSERT_TRUE(SUCCESS == init_logger());
        initial_condition = 0;
    }

    void TearDown()
    {
        shutdown_logger();
    }

    protected:
    int initial_condition;
};

class Trivial_Test_01 : public ::testing::Test
{
    void SetUp()
    {
        ASSERT_TRUE(SUCCESS == init_logger());
        initial_condition = 1;
    }

    void TearDown()
    {
        shutdown_logger();
    }

    protected:
    int initial_condition;
};

TEST_F(Trivial_Test, valid_input_1)
{
    EXPECT_TRUE(random_num()+initial_condition < 255);
}

TEST_F(Trivial_Test_01, valid_input_1)
{
    EXPECT_TRUE(random_num()+initial_condition < 255);
}

Is there a less verbose way of associating valid_input_1 with both Trivial_Test and Trivial_Test_01 ?是否有一种将valid_input_1Trivial_TestTrivial_Test_01相关联的不那么冗长的方法?

PS - The test case shown above is a trivial test which nominally represents my actual situation but nowhere close to the complexity of the testcases or testfixtures I am actually dealing with. PS - 上面显示的测试用例是一个微不足道的测试,它名义上代表了我的实际情况,但远不及我实际处理的测试用例或测试夹具的复杂性。

Did you consider value parameterized tests ?您是否考虑过值参数化测试

Maybe for your actual test cases it adds too much complexity, but your example would look like:也许对于您的实际测试用例,它增加了太多的复杂性,但您的示例看起来像:

class Trivial_Test : public ::testing::TestWithParam<int>
{
    void SetUp()
    {
         ASSERT_TRUE(SUCCESS == init_logger());
    }
    void TearDown()
    {
        shutdown_logger();
    }
};

TEST_F(Trivial_Test, valid_input)
{
    int initial_condition = GetParam();
    EXPECT_TRUE(random_num()+initial_condition < 255);
}

INSTANTIATE_TEST_CASE_P(ValidInput,
                        Trivial_Test,
                        ::testing::Values(0, 1));

You can do this using a method in a fixture class.您可以使用夹具类中的方法来执行此操作。 Here is how you would do this for you example:以下是您将如何为您执行此操作的示例:

class Trivial_Test : public ::testing::Test
{
    void SetUp()
    {
         ASSERT_TRUE(SUCCESS == init_logger());
    }
    void TearDown()
    {
        shutdown_logger();
    }
    setup_initial_condition(int value)
    {
        initial_condition = value;
    }

    protected:
    int initial_condition;
};

TEST_F(Trivial_Test, valid_input_1)
{
    setup_initial_condition(0);
    EXPECT_TRUE(random_num()+initial_condition < 255);
}

TEST_F(Trivial_Test, valid_input_2)
{
    setup_initial_condition(1);
    EXPECT_TRUE(random_num()+initial_condition < 255);
}

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

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