如何在<T>测试中实例化 MyType 中列出的 T 类 Foo 的对象?

How can I instantiate an object of class Foo<T> for Ts listed in MyTypes in a test?

本文关键字:对象 Foo MyType gt 测试 实例化 lt      更新时间:2023-10-16

我想用gtest测试一个模板类。我在谷歌测试手册中阅读了TYPED_TEST,并查看了他们引用的官方示例,但仍然无法在我的测试中实例化模板类的对象。

假设下面的简单模板类:
template <typename T>
class Foo
{
public:
    T data ;
};
    

在测试类中声明

typedef ::testing::Types<int, float> MyTypes ;
    

现在我如何在测试中为MyTypes中列出的T实例化类Foo<T>的对象?

例如:

TYPED_TEST(TestFoo, test1)
{
    Foo<T> object ;
    object.data = 1.0 ;
    
    ASSERT_FLOAT_EQ(object.data, 1.0) ;
}

在测试中,参考特殊名称TypeParam来获取类型参数。你可以输入

TYPED_TEST(TestFoo, test1)
{
    Foo<TypeParam> object ; // not Foo<T>
    object.data = 1.0 ;
    ASSERT_FLOAT_EQ(object.data, 1.0) ;
}

由于我无法自己弄清楚如何将TemplateRex的答案与Puchatek的原始代码片段结合起来,因此这里有一个完整的可编译示例。我通过阅读GTest自己的评论得到了这个,这很有帮助。

将以下内容放入test.cpp:

#include <gtest/gtest.h>
#include <regex>
#include <vector>
using vector_typelist = testing::Types<char, int*, std::regex>;
template<class> struct vector_suite : testing::Test {};
TYPED_TEST_SUITE(vector_suite, vector_typelist);
TYPED_TEST(vector_suite, bigness)
{
    std::vector<TypeParam> v;
    EXPECT_TRUE(sizeof(v) >= sizeof(TypeParam));
}

使用适当的-I-l选项编译。这可能看起来像:

g++ -std=c++14 -I/usr/local/include test.cpp -L/usr/local/lib -lgtest -lgtest_main

或者(如果您使用pkg-config):

g++ -std=c++14 $(pkg-config --cflags gtest) test.cpp $(pkg-config --libs gtest_main)

运行可执行文件,你应该看到如下内容:

$ ./a.out
Running main() from /foobar/gtest_main.cc
[==========] Running 3 tests from 3 test suites.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN      ] vector_suite/0.bigness
[       OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)
[----------] 1 test from vector_suite/1, where TypeParam = int*
[ RUN      ] vector_suite/1.bigness
[       OK ] vector_suite/1.bigness (0 ms)
[----------] 1 test from vector_suite/1 (0 ms total)
[----------] 1 test from vector_suite/2, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >
[ RUN      ] vector_suite/2.bigness
test.cpp:12: Failure
Value of: sizeof(v) >= sizeof(TypeParam)
  Actual: false
Expected: true
[  FAILED  ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> > (0 ms)
[----------] 1 test from vector_suite/2 (0 ms total)
[----------] Global test environment tear-down
[==========] 3 tests from 3 test suites ran. (0 ms total)
[  PASSED  ] 2 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] vector_suite/2.bigness, where TypeParam = std::basic_regex<char, std::__1::regex_traits<char> >
 1 FAILED TEST

(我放入这个失败的测试纯粹是为了演示当类型化测试失败时会发生什么。事实上,sizeof(std::vector<std::regex>)确实比sizeof(std::regex)小。


请注意,TYPED_TEST产生了一系列具有相关但不相同名称的测试套件。没有单独的vector_suite了:

$ ./a.out --gtest_filter=vector_suite.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite.*
[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

vector_suite/0, vector_suite/1vector_suite/2:

$ ./a.out --gtest_filter=vector_suite/0.*
Running main() from /foobar/gtest_main.cc
Note: Google Test filter = vector_suite/0.*
[==========] Running 1 test from 1 test suite.
[----------] Global test environment set-up.
[----------] 1 test from vector_suite/0, where TypeParam = char
[ RUN      ] vector_suite/0.bigness
[       OK ] vector_suite/0.bigness (0 ms)
[----------] 1 test from vector_suite/0 (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test suite ran. (0 ms total)
[  PASSED  ] 1 test.