使用INSTANTIATE_TEST_CASE_P的同一夹具的不同实例
Different instances for the same Fixture using INSTANTIATE_TEST_CASE_P
我们是否可以使用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();
相关文章:
- 从C++实例化QML
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 如何在c++中为模板函数实例创建快捷方式
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 设计一个只能由特定类实例化的类(如果可能的话,通过make_unique)
- 如何创建一个空的全局类并在启动时实例化它
- 无法创建抽象类的实例
- 多个文件的内存分配错误"在抛出 'std :: bad_alloc' what (): std :: bad_alloc 的实例后终止调用" [C++]
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我收到以下错误:抛出'std::bad_alloc'实例后终止调用
- 建议在运行时将带有类实例的列表从c++导入qml
- 约束和显式模板实例化
- 通过实例理解std::move及其目的
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 正在生成未知类实例
- 从DLL中删除类的实例
- 使用INSTANTIATE_TEST_CASE_P的同一夹具的不同实例
- 不可实例化的谷歌测试夹具超级类