Различные экземпляры для одного и того же прибора с использованием INSTANTIATE_TEST_CASE_P - PullRequest
0 голосов
/ 19 октября 2018

Можем ли мы иметь два разных экземпляра для одного и того же ИСПЫТАНИЯ, используя INSTANTIATE_TEST_CASE_P

1 Ответ

0 голосов
/ 20 октября 2018

Конечно.Вот простейший пример:

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.

В болееСценарий likelife: -

my_fixture разработан программистом Алисой и реализован в файле заголовка my_fixture.h и (при необходимости) в библиотеке lib_myfixture.

Тесты Coloursразработаны программистом Бобом в отдельном наборе тестов, реализованном в: -

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 тесты разработаны программистом Кэрол в другом отдельном наборе тестов, реализованном таким же образом.

Позже

Что я хотел знать, так это то, можно ли связать экземпляр с конкретными 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();
.
...