本文介绍了使用GTest的C ++多个参数TYPED_TEST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些休闲测试:

TEST_F(FactoryShould, createAFromAModule)
{
    const auto stateMachine = createStateMachine(EModule_A);
    const auto* typedStateMachine =
        dynamic_cast<const BackEnd<transitiontable::A, Guard>*>(stateMachine.get());
    ASSERT_TRUE(typedStateMachine);
}

TEST_F(FactoryShould, createBFromBModule)
{
    const auto stateMachine = createStateMachine(EModule_B);
    const auto* typedStateMachine =
        dynamic_cast<const BackEnd<transitiontable::B, Guard>*>(stateMachine.get());
    ASSERT_TRUE(typedStateMachine);
}

有什么方法可以将它们转变为类型化测试?我所看到的只是对一个更改参数的解决方案,我有2个更改参数,EModule可用于多个过渡表,因此诸如map的外观看起来不错,但这是可行的吗?

Is there any way to turn them into Typed Tests? All I seen is solution for only one changing parameter, there I have 2 changing parameters, EModule can be used for multiple transitiontables, so something like map looks good, but it is doable?

推荐答案

使用 std::pair 从其他两种中选出一种. (并且使用 std::tuple 您可以从任何其他 N 中创建一种类型.

With std::pair you canmake one type out of any other two. (And with std::tupleyou can make one type out of any other N).

您可以编写googletest TYPED_TEST,其中TypeParam假定来自std::pair<X,Y>的列表,用于成对的参数类型XY,这样,每个TYPED_TEST的实例化将X定义为TypeParam::first_type,将Y定义为TypeParam::second_type.例如:

You can write googletest TYPED_TESTs in which TypeParam assumes values froma list of std::pair<X,Y>, for paired parameter-types X and Y, so that each instantiation of such a TYPED_TESThas X defined as TypeParam::first_type and Y defined as TypeParam::second_type. E.g:

gtester.cpp

#include <gtest/gtest.h>
#include <utility>
#include <cctype>

struct A1 {
    char ch = 'A';
};

struct A2 {
    char ch = 'a';
};

struct B1 {
    char ch = 'B';
};

struct B2 {
    char ch = 'b';
};


template <typename T>
class pair_test : public ::testing::Test {};

using test_types = ::testing::Types<std::pair<A1,A2>, std::pair<B1,B2>>;
TYPED_TEST_CASE(pair_test, test_types);

TYPED_TEST(pair_test, compare_no_case)
{
    typename TypeParam::first_type param1;
    typename TypeParam::second_type param2;
    ASSERT_TRUE(param1.ch == std::toupper(param2.ch));
}

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

编译,链接,运行:

$ g++ -Wall -o gtester gtester.cpp -lgtest -pthread && ./gtester
[==========] Running 2 tests from 2 test cases.
[----------] Global test environment set-up.
[----------] 1 test from pair_test/0, where TypeParam = std::pair<A1, A2>
[ RUN      ] pair_test/0.compare_no_case
[       OK ] pair_test/0.compare_no_case (0 ms)
[----------] 1 test from pair_test/0 (0 ms total)

[----------] 1 test from pair_test/1, where TypeParam = std::pair<B1, B2>
[ RUN      ] pair_test/1.compare_no_case
[       OK ] pair_test/1.compare_no_case (0 ms)
[----------] 1 test from pair_test/1 (0 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 2 test cases ran. (0 ms total)
[  PASSED  ] 2 tests.

这篇关于使用GTest的C ++多个参数TYPED_TEST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 19:30