- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
假设我在头文件 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();
}
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.
您看到了 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
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();
}
重新编译、重新链接和重新运行:
$ 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/
假设我在头文件 fixtures.h 中定义了一个 GTest fixture: class baseFixture : public ::testing::Test{ // some sha
我们可以使用 INSTANTIATE_TEST_CASE_P 为同一个 TEST FIXTURE 设置两个不同的实例 最佳答案 当然。这是一个基本示例: gtester.cpp #include #
我想将整数数组列表作为容器传递给 GTEST 中的参数化类。 例如:- const int* par_Array[] = { { 1, 869, 1200, 1300 },
我是一名优秀的程序员,十分优秀!