GTest 的 C++ 多个参数 TYPED_TEST
C++ Multiple parameters with GTest 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 这样的东西看起来不错,但它是可行的吗?
用std::pair
你可以
从任何其他两个中创建一个类型。 (还有 std::tuple
你可以从任何其他 N).
您可以编写 googletest TYPED_TEST
s,其中 TypeParam
假定值来自
std::pair<X,Y>
的列表,用于成对的参数类型 X
和 Y
,这样 TYPED_TEST
的每个实例化
X
定义为 TypeParam::first_type
,Y
定义为 TypeParam::second_type
。例如:
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();
}
编译,link,运行:
$ 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.
我有以下几组测试:
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 这样的东西看起来不错,但它是可行的吗?
用std::pair
你可以
从任何其他两个中创建一个类型。 (还有 std::tuple
你可以从任何其他 N).
您可以编写 googletest TYPED_TEST
s,其中 TypeParam
假定值来自
std::pair<X,Y>
的列表,用于成对的参数类型 X
和 Y
,这样 TYPED_TEST
的每个实例化
X
定义为 TypeParam::first_type
,Y
定义为 TypeParam::second_type
。例如:
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();
}
编译,link,运行:
$ 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.