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

时间:2018-10-19 14:35:37

标签: c++ unit-testing c++11 googletest

我们可以使用INSTANTIATE_TEST_CASE_P对同一测试夹具有两个不同的实例

1 个答案:

答案 0 :(得分:0)

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

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开发,另外 测试套件,以相同的方式实现。

稍后

  

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

不,您不能这样做,因为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();