为模板类创建gtest值参数化测试 [英] Create gtest value parametrized test for a template class

查看:151
本文介绍了为模板类创建gtest值参数化测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型的模板类

<size_t N>
class Line {
   ...
};

如何为该类的多个实例创建测试?例如第< 1>,第< 2>,...

How could I could I create a test for several instances of this class? e.g. Line<1>, Line<2>, ...

我已经检查了文档,并且有:

I have checked the documentation and there are:

  • 值参数化测试,用于运行时值

  • Value-Parametrized tests, for run-time values

类型参数化测试,使您可以在编译时更改类型名称.

Type-Parametrized tests, which allow you to change the typename at compile time.

但是,我在编译时没有发现任何可以更改值的内容.

However, I do not find anything to change values at compile-time.

推荐答案

Googletest没有可用于生成测试结果的现成解决方案 所需的一组非类型模板参数的模板.

Googletest has no ready-to-wear solution for generating tests over a template for a set of non-type template parameters, as you want.

假设使用C ++ 11或更高版本,则可以利用 std::integral_constant<T,N> 创建一个唯一表示std::size_t范围中每个范围的类 您要对其进行测试以覆盖Line<N>的参数N,并使用std::integral_constant<std::size_t,N> 作为N的代理以构造TYPED_TEST_CASE和关联的TYPED_TEST.像这样:

Assuming C++11 or later, you can take advantage of std::integral_constant<T,N> to create a class that uniquely represents each of the range of std::size_t parameters N for which you want tests covering Line<N>, and use std::integral_constant<std::size_t,N> as a proxy for N to construct a TYPED_TEST_CASE and associated TYPED_TESTs. Like so:

用于说明的玩具Line类:

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测试运行器:

Googletest test runner:

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.

这篇关于为模板类创建gtest值参数化测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆