簡體   English   中英

使用INSTANTIATE_TEST_CASE_P針對同一燈具的不同實例

[英]Different instances for the same Fixture using INSTANTIATE_TEST_CASE_P

我們可以使用INSTANTIATE_TEST_CASE_P對同一測試夾具有兩個不同的實例嗎

當然。 這是一個基本示例:

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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM