gpt4 book ai didi

c++ - 使用 INSTANTIATE_TEST_CASE_P 的同一 Fixture 的不同实例

转载 作者:太空宇宙 更新时间:2023-11-04 12:43:29 42 4
gpt4 key购买 nike

我们可以使用 INSTANTIATE_TEST_CASE_P 为同一个 TEST FIXTURE 设置两个不同的实例

最佳答案

当然。这是一个基本示例:

gtester.cpp

#include <gtest/gtest.h>
#include <string>

class my_fixture :
public ::testing::TestWithParam<std::string> {
};


INSTANTIATE_TEST_CASE_P(Colours,my_fixture,
::testing::Values("red", "green", "blue"));

INSTANTIATE_TEST_CASE_P(Shapes,my_fixture,
::testing::Values("square", "circle", "triangle"));


TEST_P(my_fixture, has_positive_size) {
auto const & val = GetParam();
ASSERT_TRUE(val.size() > 0);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

编译:

$ g++ -std=c++11 -Wall -Wextra -c gtester.cpp

链接:

$ g++ -o gtester gtester.o -lgtest -pthread

运行:

$ ./gtester
[==========] Running 6 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from Colours/my_fixture
[ RUN ] Colours/my_fixture.has_positive_size/0
[ OK ] Colours/my_fixture.has_positive_size/0 (0 ms)
[ RUN ] Colours/my_fixture.has_positive_size/1
[ OK ] Colours/my_fixture.has_positive_size/1 (0 ms)
[ RUN ] Colours/my_fixture.has_positive_size/2
[ OK ] Colours/my_fixture.has_positive_size/2 (0 ms)
[----------] 3 tests from Colours/my_fixture (0 ms total)

[----------] 3 tests from Shapes/my_fixture
[ RUN ] Shapes/my_fixture.has_positive_size/0
[ OK ] Shapes/my_fixture.has_positive_size/0 (0 ms)
[ RUN ] Shapes/my_fixture.has_positive_size/1
[ OK ] Shapes/my_fixture.has_positive_size/1 (0 ms)
[ RUN ] Shapes/my_fixture.has_positive_size/2
[ OK ] Shapes/my_fixture.has_positive_size/2 (0 ms)
[----------] 3 tests from Shapes/my_fixture (0 ms total)

[----------] Global test environment tear-down
[==========] 6 tests from 2 test cases ran. (0 ms total)
[ PASSED ] 6 tests.

仅运行Colours 测试:

$ ./gtester --gtest_filter=Colours/* 
Note: Google Test filter = Colours/*
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from Colours/my_fixture
[ RUN ] Colours/my_fixture.has_positive_size/0
[ OK ] Colours/my_fixture.has_positive_size/0 (0 ms)
[ RUN ] Colours/my_fixture.has_positive_size/1
[ OK ] Colours/my_fixture.has_positive_size/1 (0 ms)
[ RUN ] Colours/my_fixture.has_positive_size/2
[ OK ] Colours/my_fixture.has_positive_size/2 (0 ms)
[----------] 3 tests from Colours/my_fixture (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (1 ms total)
[ PASSED ] 3 tests.

仅运行 Shapes 测试:

$ ./gtester --gtest_filter=Shapes/* 
Note: Google Test filter = Shapes/*
[==========] Running 3 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 3 tests from Shapes/my_fixture
[ RUN ] Shapes/my_fixture.has_positive_size/0
[ OK ] Shapes/my_fixture.has_positive_size/0 (0 ms)
[ RUN ] Shapes/my_fixture.has_positive_size/1
[ OK ] Shapes/my_fixture.has_positive_size/1 (0 ms)
[ RUN ] Shapes/my_fixture.has_positive_size/2
[ OK ] Shapes/my_fixture.has_positive_size/2 (0 ms)
[----------] 3 tests from Shapes/my_fixture (0 ms total)

[----------] Global test environment tear-down
[==========] 3 tests from 1 test case ran. (0 ms total)
[ PASSED ] 3 tests.

在更接近生活的场景中:-

my_fixture 由程序员 Alice 开发,并在头文件 my_fixture.h 和(如果需要)库 lib_myfixture 中实现。

Colours 测试由程序员 Bob 在一个单独的测试套件中开发:-

colour_test.cpp

#include <my_fixture.h>

INSTANTIATE_TEST_CASE_P(Colours,my_fixture,
::testing::Values("red", "green", "blue"));

TEST_P(my_fixture, ...) {
...
}

...

构建方式如下:

$ g++ -std=c++11 -Wall -Wextra -I/my_fixture/include/path -c colour_test.cpp
$ g++ -o colour_test colour_test.o -L/my_fixture/lib/path -lmy_fixture -lgtest -pthread

Shapes 测试是由程序员 Carol 在另一个单独的项目中开发的测试套件,以相同的方式实现。

稍后

What I wanted to know is if it is possible to couple an instantiation with specific TEST_Ps... and another one coupled with another set of TEST_Ps for the same test fixture.

不,你不能那样做,因为 TEST_P(fixture,description) 被绑定(bind)到fixture,而不是 fixture 的实例化。但考虑到任何固定装置制作两个功能相同但实例化不同的装置很简单,例如

#include <gtest/gtest.h>
#include <string>

class my_fixture :
public ::testing::TestWithParam<std::string> {
};

class colours_fixture : public my_fixture {};
class shapes_fixture : public my_fixture {};


INSTANTIATE_TEST_CASE_P(Colours,colours_fixture,
::testing::Values("red", "green", "blue"));

INSTANTIATE_TEST_CASE_P(Shapes,shapes_fixture,
::testing::Values("square", "circle", "triangle"));


TEST_P(colours_fixture, has_positive_size) {
auto const & val = GetParam();
ASSERT_TRUE(val.size() > 0);
}

TEST_P(shapes_fixture, has_positive_size) {
auto const & val = GetParam();
ASSERT_TRUE(val.size() > 0);
}

int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();

关于c++ - 使用 INSTANTIATE_TEST_CASE_P 的同一 Fixture 的不同实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52894593/

42 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com