简体   繁体   English

使用INSTANTIATE_TEST_CASE_P针对同一灯具的不同实例

[英]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.

相关问题 当在单独的可执行文件中调用 INSTANTIATE_TEST_CASE_P 时,库中的 TEST_P 测试不会运行 - TEST_P tests in library do not run when INSTANTIATE_TEST_CASE_P is called in a separate executable 如何在具有多个测试.cpps的头文件中使用INSTANTIATE_TEST_CASE_P? - How to use INSTANTIATE_TEST_CASE_P in header file with multiple test .cpps? GTEST:如何在 INSANTIATE_TEST_CASE_P 的 ValuesIn 中将数组列表作为参数传递? - GTEST : How to pass a List of Arrays as a parameter in ValuesIn of INSTANTIATE_TEST_CASE_P? 如何为 INSTANTIATE_TEST_CASE_P 参数化单元测试动态创建大量数据 - How to dynamically create a lot of data for INSTANTIATE_TEST_CASE_P parameterized unit tests 每次使用不同的夹具多次执行一个测试用例 - Execute one test case several times with different fixture each time 测试例相同,但代码块和Ideone上的输出不同 - Same test case but different output on codeblocks and Ideone googletest:是否可以选择单个测试夹具/案例? - googletest: is it possible to select a single test fixture/case? 带有夹具支持的 BOOST_DATA_TEST_CASE - BOOST_DATA_TEST_CASE with fixture support 如何使用不同的谷歌模拟运行相同的谷歌测试用例? - How to run the same google test case with different google mocks? 使用夹具时升压单元测试过滤器 - boost unit test filter when using a fixture
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM