gpt4 book ai didi

c++ - 如何在具有多个测试 .cpps 的头文件中使用 INSTANTIATE_TEST_CASE_P?

转载 作者:太空宇宙 更新时间:2023-11-04 12:39:06 26 4
gpt4 key购买 nike

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

 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 文件中的大量测试进行参数化。我想在标题中写:

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

为了对 {true, false} 和 {1} 的叉积运行一组测试。当我只编译一个 cpp 文件,将其命名为 N1.cpp 并运行可执行文件时,我的 TEST_P(derivedFixture* 测试得到了正确的行为 - 它们每次运行两次。但是,当我构建整个项目并执行我的测试,每个测试运行 2 * N 次。我在头文件中使用了 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();
}

ma​​in.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.

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

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

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

fixture.hpp 中,这是 #include 编辑的,因此在每个翻译单元 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

ma​​in.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();
}

重新编译、重新链接和重新运行:

$ 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.

关于c++ - 如何在具有多个测试 .cpps 的头文件中使用 INSTANTIATE_TEST_CASE_P?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55129349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com