[英]Different instances for the same Fixture using INSTANTIATE_TEST_CASE_P
我们可以使用INSTANTIATE_TEST_CASE_P对同一测试夹具有两个不同的实例吗
Certainly. 当然。 Here is an elementary example: 这是一个基本示例:
gtester.cpp 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();
}
Compile: 编译:
$ g++ -std=c++11 -Wall -Wextra -c gtester.cpp
Link: 链接:
$ g++ -o gtester gtester.o -lgtest -pthread
Run: 跑:
$ ./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.
Run the Colours
tests only: 仅运行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.
Run the Shapes
tests only: 仅运行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.
In a more likelife scenario:- 在更像人生的场景中:
my_fixture
is developed by programmer Alice, and implemented in a header file my_fixture.h
and (if necessary) a library lib_myfixture
. my_fixture
由程序员Alice开发,并在头文件my_fixture.h
和(如果需要)库lib_myfixture
。
The Colours
tests are developed by programmer Bob, in a separate test-suite implemented in:- Colours
测试是由程序员Bob在单独的测试套件中开发的,该套件通过以下方式实现:
colour_test.cpp colour_test.cpp
#include <my_fixture.h>
INSTANTIATE_TEST_CASE_P(Colours,my_fixture,
::testing::Values("red", "green", "blue"));
TEST_P(my_fixture, ...) {
...
}
...
which is built like: 它的构建方式如下:
$ 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
And the Shapes
tests are developed by programmer Carol, in another separate test-suite, implemented in the same way. Shapes
测试由程序员Carol在另一个单独的测试套件中开发,以相同的方式实现。
Later 后来
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耦合在一起,并将另一个实例与同一测试夹具的另一组TEST_P耦合在一起。
No, you can't do that, because a TEST_P(fixture,description)
is bound to fixture
, not to an instantiation of fixture
. 不,你不能这样做,因为TEST_P(fixture,description)
势必fixture
,而不是一个实例fixture
。 But given any fixture it is trivial to make two functionally identical fixtures that are instantiated differently, eg 但是给定任何灯具,制作两个功能相同的灯具却是不同的实例是很简单的,例如
#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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.