你正在做的实质上是这样的:
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();
}
编译、链接和运行:
$ ./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.
当你期望只有4个测试时,却看到了8个测试,每个instantiation_one/fixture.test_a/N
都被运行了两次,其中N
为{0,1}。
这个bug的问题在于:
INSTANTIATE_TEST_CASE_P(instantiation_one, fixture,
::testing::Combine(::testing::Bool(), ::testing::Values(1)))
在
fixture.hpp
中,该文件被
#include
并在每个翻译单元中重复出现,导致此代码注册的两个参数化测试在运行时会被注册
N
次,因此会运行N次。我们应该为给定值集合的每个实例化值参数化 fixture 只编译一次,因此只需在一个源文件中完成,例如:
fixture.hpp(2)。
#ifndef FIXTURE_HPP
#define FIXTURE_HPP
#include <gtest/gtest.h>
struct fixture: ::testing::TestWithParam<std::tuple<bool, int>>
{};
#endif
#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();
}
重新编译、链接并运行:
$ 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.
foo.h
的内容进行预处理和编译多次,无论foo.h
被#include
了多少次。如果您编译了N个翻译单元,每个单元都包含了#include foo.h
并成功将N个目标文件链接到一个程序中,那么由于编译foo.h
内容而生成的任何代码都将被链接到该程序中N次。 - Mike Kinghan