使用INSTANTIATE_TEST_CASE_P的同一夹具的不同实例

Different instances for the same Fixture using INSTANTIATE_TEST_CASE_P

本文关键字:实例 夹具 INSTANTIATE TEST CASE 使用      更新时间:2023-10-16

我们是否可以使用INSTANTIATE_TEST_CASE_P为同一个测试夹具设置两个不同的实例

当然。下面是一个基本示例:

格斯特.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在另一个单独的测试中开发的。 测试套件,以相同的方式实现。

我想知道的是,是否有可能将一个实例与特定TEST_Ps耦合......另一个实例与另一组TEST_Ps耦合到同一测试夹具。

不,你不能那样做,因为TEST_P(fixture,description)必然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();