为模板创建 gtest 值参数化测试 class
Create gtest value parametrized test for a template class
我有一个 class 类型的模板
<size_t N>
class Line {
...
};
如何为这个 class 的多个实例创建测试?例如行<1>,行<2>,...
我查看了文档,有:
值参数化测试,运行 时间值
类型参数化测试,允许您在编译时更改类型名称。
但是,我没有发现任何可以在编译时更改值的东西。
Googletest 没有用于生成测试的成衣解决方案
一组非类型模板参数的模板,如您所愿。
假设是 C++11 或更高版本,你可以利用 std::integral_constant<T,N>
创建一个 class 来唯一代表 std::size_t
的每个范围
参数 N
,您希望对其进行涵盖 Line<N>
的测试,并使用 std::integral_constant<std::size_t,N>
作为 N
的代理来构建 TYPED_TEST_CASE
和关联的 TYPED_TEST
。像这样:
一个玩具Line
class作图:
Line.h
#ifndef LINE_H
#define LINE_H
#include <cstddef>
template <std::size_t N>
class Line {
public:
static constexpr std::size_t capacity() {
return N;
}
explicit Line(std::size_t n)
: _length{n < N ? n : N}{};
std::size_t length() const {
return _length;
}
private:
std::size_t _length = 0;
};
#endif
Googletest 测试运行程序:
line_test.cpp
#include <gtest/gtest.h>
#include <type_traits>
#include "line.h"
template <typename T>
class line_tester : public ::testing::Test{};
using test_types = ::testing::Types<
std::integral_constant<std::size_t,2>,
std::integral_constant<std::size_t,3>,
std::integral_constant<std::size_t,5>>;
TYPED_TEST_CASE(line_tester, test_types);
TYPED_TEST(line_tester, get_capacity) {
static constexpr std::size_t n = TypeParam::value;
ASSERT_EQ(n,Line<n>::capacity());
}
TYPED_TEST(line_tester, set_length_preserved) {
Line<TypeParam::value> line{1};
ASSERT_EQ(line.length(),1);
}
TYPED_TEST(line_tester, set_length_trunctated) {
static constexpr std::size_t n = TypeParam::value;
Line<n> line{999};
ASSERT_EQ(line.length(),Line<n>::capacity());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
运行方式如下:
$ ./line_tester
[==========] Running 9 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from line_tester/0, where TypeParam = std::integral_constant<unsigned long, 2ul>
[ RUN ] line_tester/0.get_capacity
[ OK ] line_tester/0.get_capacity (0 ms)
[ RUN ] line_tester/0.set_length_preserved
[ OK ] line_tester/0.set_length_preserved (0 ms)
[ RUN ] line_tester/0.set_length_trunctated
[ OK ] line_tester/0.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/0 (1 ms total)
[----------] 3 tests from line_tester/1, where TypeParam = std::integral_constant<unsigned long, 3ul>
[ RUN ] line_tester/1.get_capacity
[ OK ] line_tester/1.get_capacity (0 ms)
[ RUN ] line_tester/1.set_length_preserved
[ OK ] line_tester/1.set_length_preserved (0 ms)
[ RUN ] line_tester/1.set_length_trunctated
[ OK ] line_tester/1.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/1 (0 ms total)
[----------] 3 tests from line_tester/2, where TypeParam = std::integral_constant<unsigned long, 5ul>
[ RUN ] line_tester/2.get_capacity
[ OK ] line_tester/2.get_capacity (0 ms)
[ RUN ] line_tester/2.set_length_preserved
[ OK ] line_tester/2.set_length_preserved (0 ms)
[ RUN ] line_tester/2.set_length_trunctated
[ OK ] line_tester/2.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/2 (0 ms total)
[----------] Global test environment tear-down
[==========] 9 tests from 3 test cases ran. (1 ms total)
[ PASSED ] 9 tests.
我有一个 class 类型的模板
<size_t N>
class Line {
...
};
如何为这个 class 的多个实例创建测试?例如行<1>,行<2>,...
我查看了文档,有:
值参数化测试,运行 时间值
类型参数化测试,允许您在编译时更改类型名称。
但是,我没有发现任何可以在编译时更改值的东西。
Googletest 没有用于生成测试的成衣解决方案 一组非类型模板参数的模板,如您所愿。
假设是 C++11 或更高版本,你可以利用 std::integral_constant<T,N>
创建一个 class 来唯一代表 std::size_t
的每个范围
参数 N
,您希望对其进行涵盖 Line<N>
的测试,并使用 std::integral_constant<std::size_t,N>
作为 N
的代理来构建 TYPED_TEST_CASE
和关联的 TYPED_TEST
。像这样:
一个玩具Line
class作图:
Line.h
#ifndef LINE_H
#define LINE_H
#include <cstddef>
template <std::size_t N>
class Line {
public:
static constexpr std::size_t capacity() {
return N;
}
explicit Line(std::size_t n)
: _length{n < N ? n : N}{};
std::size_t length() const {
return _length;
}
private:
std::size_t _length = 0;
};
#endif
Googletest 测试运行程序:
line_test.cpp
#include <gtest/gtest.h>
#include <type_traits>
#include "line.h"
template <typename T>
class line_tester : public ::testing::Test{};
using test_types = ::testing::Types<
std::integral_constant<std::size_t,2>,
std::integral_constant<std::size_t,3>,
std::integral_constant<std::size_t,5>>;
TYPED_TEST_CASE(line_tester, test_types);
TYPED_TEST(line_tester, get_capacity) {
static constexpr std::size_t n = TypeParam::value;
ASSERT_EQ(n,Line<n>::capacity());
}
TYPED_TEST(line_tester, set_length_preserved) {
Line<TypeParam::value> line{1};
ASSERT_EQ(line.length(),1);
}
TYPED_TEST(line_tester, set_length_trunctated) {
static constexpr std::size_t n = TypeParam::value;
Line<n> line{999};
ASSERT_EQ(line.length(),Line<n>::capacity());
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
运行方式如下:
$ ./line_tester
[==========] Running 9 tests from 3 test cases.
[----------] Global test environment set-up.
[----------] 3 tests from line_tester/0, where TypeParam = std::integral_constant<unsigned long, 2ul>
[ RUN ] line_tester/0.get_capacity
[ OK ] line_tester/0.get_capacity (0 ms)
[ RUN ] line_tester/0.set_length_preserved
[ OK ] line_tester/0.set_length_preserved (0 ms)
[ RUN ] line_tester/0.set_length_trunctated
[ OK ] line_tester/0.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/0 (1 ms total)
[----------] 3 tests from line_tester/1, where TypeParam = std::integral_constant<unsigned long, 3ul>
[ RUN ] line_tester/1.get_capacity
[ OK ] line_tester/1.get_capacity (0 ms)
[ RUN ] line_tester/1.set_length_preserved
[ OK ] line_tester/1.set_length_preserved (0 ms)
[ RUN ] line_tester/1.set_length_trunctated
[ OK ] line_tester/1.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/1 (0 ms total)
[----------] 3 tests from line_tester/2, where TypeParam = std::integral_constant<unsigned long, 5ul>
[ RUN ] line_tester/2.get_capacity
[ OK ] line_tester/2.get_capacity (0 ms)
[ RUN ] line_tester/2.set_length_preserved
[ OK ] line_tester/2.set_length_preserved (0 ms)
[ RUN ] line_tester/2.set_length_trunctated
[ OK ] line_tester/2.set_length_trunctated (0 ms)
[----------] 3 tests from line_tester/2 (0 ms total)
[----------] Global test environment tear-down
[==========] 9 tests from 3 test cases ran. (1 ms total)
[ PASSED ] 9 tests.