如何在具有多个测试 .cpps 的 header 文件中使用 INSTANTIATE_TEST_CASE_P?

How to use INSTANTIATE_TEST_CASE_P in header file with multiple test .cpps?

假设我在 header 文件中定义了一个 GTest fixture fixtures.h:

 class baseFixture : public ::testing::Test{
   // some shared functionality among tests
 }

以及允许进行某些参数化的派生夹具:

class derivedFixture: public baseFixture, 
  public ::testing::WithParamInterface<std::tuple<bool, int>>{};

我想与 INSTANTIATE_TEST_CASE_P 一起使用,以便对分布在 N 个 .cpp 文件中的大量测试进行参数化。我想写在header:

INSTANTIATE_TEST_CASE_P(derivedTests, derivedFixture, 
  ::testing::Combine(::testing::Bool(), ::testing::Values(1));

为了 运行 {true, false} 和 {1} 的叉积测试集。当我只编译一个 cpp 文件,将其命名为 N1.cpp 和 运行 可执行文件时,我的 TEST_P(derivedFixture* 测试得到了正确的行为 - 它们每个 运行 两次。但是,当我构建整个项目并执行我的测试时,每个测试都是 运行 2 * N 次。我在 header 文件中使用了 include guards 来防止 INSTANTIATE 宏被调用两次,并且我肯定不会在其他任何地方调用它。

你所做的本质上是这样的:

fixture.hpp (1)

#ifndef FIXTURE_HPP
#define FIXTURE_HPP

#include <gtest/gtest.h>

struct fixture: ::testing::TestWithParam<std::tuple<bool, int>>
{};

INSTANTIATE_TEST_CASE_P(instantiation_one, fixture,
  ::testing::Combine(::testing::Bool(), ::testing::Values(1)));

#endif

t1.cpp

#include "fixture.hpp"
#include <tuple>

TEST_P(fixture, test_a)
{
    auto const & param = GetParam();
    std::cout << "param 0 = " << std::get<0>(param) << std::endl;
    std::cout << "param 1 = " << std::get<1>(param) << std::endl;
    SUCCEED();
}

t2.cpp

#include "fixture.hpp"
#include <tuple>

TEST_P(fixture, test_b)
{
    auto const & param = GetParam();
    std::cout << "param 0 = " << std::get<0>(param) << std::endl;
    std::cout << "param 1 = " << std::get<1>(param) << std::endl;
    SUCCEED();
}

main.cpp (1)

#include <gtest/gtest.h>

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

编译,link和运行:

$ ./gtester
[==========] Running 8 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 8 tests from instantiation_one/fixture
[ RUN      ] instantiation_one/fixture.test_a/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/0 (0 ms)
[ RUN      ] instantiation_one/fixture.test_a/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/1 (0 ms)
[ RUN      ] instantiation_one/fixture.test_a/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/0 (0 ms)
[ RUN      ] instantiation_one/fixture.test_a/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/1 (0 ms)
[ RUN      ] instantiation_one/fixture.test_b/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/0 (0 ms)
[ RUN      ] instantiation_one/fixture.test_b/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/1 (0 ms)
[ RUN      ] instantiation_one/fixture.test_b/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/0 (0 ms)
[ RUN      ] instantiation_one/fixture.test_b/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/1 (0 ms)
[----------] 8 tests from instantiation_one/fixture (0 ms total)

[----------] Global test environment tear-down
[==========] 8 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 8 tests.

您看到了 8 个测试,而您预期是 4 个,每个 instantiation_one/fixture.test_a/N 是 运行 两次,对于 {0,1} 中的 N

错误是这样的:我们这样做:

INSTANTIATE_TEST_CASE_P(instantiation_one, fixture,
  ::testing::Combine(::testing::Bool(), ::testing::Values(1)));

in fixture.hpp,这是 #include-ed,因此在 every 翻译单元 tN.cpp,导致注册的 2 个参数化测试 通过此代码在 运行 次被注册 N 次,因此 运行 N 次。

我们应该编译值参数化夹具的每个实例 对于一组给定的值仅一次,因此仅在一个源文件中进行,例如

fixture.hpp (2)

#ifndef FIXTURE_HPP
#define FIXTURE_HPP

#include <gtest/gtest.h>

struct fixture: ::testing::TestWithParam<std::tuple<bool, int>>
{};


#endif

main.cpp (2)

#include <gtest/gtest.h>
#include "fixture.hpp"

INSTANTIATE_TEST_CASE_P(instantiation_one, fixture,
  ::testing::Combine(::testing::Bool(), ::testing::Values(1)));

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

重新编译,重新link和重新运行:

$ g++ -Wall -Wextra -c main.cpp t1.cpp t2.cpp
$ g++ -o gtester main.o t1.o t2.o -lgtest -pthread
$ ./gtester
[==========] Running 4 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 4 tests from instantiation_one/fixture
[ RUN      ] instantiation_one/fixture.test_a/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/0 (0 ms)
[ RUN      ] instantiation_one/fixture.test_a/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_a/1 (0 ms)
[ RUN      ] instantiation_one/fixture.test_b/0
param 0 = 0
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/0 (1 ms)
[ RUN      ] instantiation_one/fixture.test_b/1
param 0 = 1
param 1 = 1
[       OK ] instantiation_one/fixture.test_b/1 (0 ms)
[----------] 4 tests from instantiation_one/fixture (1 ms total)

[----------] Global test environment tear-down
[==========] 4 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 4 tests.