如何在 Google 测试中使用不同模板测试多个模板化 类 的相同行为?

How to test the same behaviour for multiple templated classes with different templates in Google Test?

我正在用 C++ 17 练习排序算法,并按如下方式实现了我的单元测试(以下编译和所有测试都是绿色的):

template <typename T>
class SortingmethodTest : public ::testing::Test
{
protected:   
    T sortingmethod;

    static constexpr int amount_test_data[7] = {0, 4, 8, 10, 256, 1000, 1234};
};

using sortingmethods = ::testing::Types<STLSort<int>,
                                         InsertionSort<int>,
                                         ShellSort<int>,
                                         MergeSort<int>,
                                         OptimizedMergeSort<int>,
                                         QuickSort<int>>;

TYPED_TEST_SUITE(SortingmethodTest, sortingmethods);

TYPED_TEST(SortingmethodTest, sort)
{
    for (const auto& amount : this->amount_test_data)
    {
        Sortvector<int> test(amount);
        test.vul_random_zonder_dubbels(); // Fills the vector

        this->sortingmethod(test); // operator() of the sortmethod used (STLSort, InsertionSort, ...) sorts the vector

        ASSERT_TRUE(test.is_range());
        ASSERT_TRUE(test.is_gesorteerd());
        ASSERT_TRUE(std::is_sorted(test.begin(), test.end()));
    }
}

TYPED_TEST(SortingmethodTest, sort_reverse)
{
    // ...
}

TYPED_TEST(SortingmethodTest, sort_already_sorted)
{
    // ...
}

TYPED_TEST(SortingmethodTest, sort_empty)
{
    // ...
}

我想对整数以外的其他类型重复相同的测试,例如

STLSort<int>,
InsertionSort<int>,
ShellSort<int>,
MergeSort<int>,
OptimizedMergeSort<int>,
QuickSort<int>

STLSort<double>,
InsertionSort<double>,
ShellSort<double>,
MergeSort<double>,
OptimizedMergeSort<double>,
QuickSort<double>

STLSort<CustomType>,
InsertionSort<CustomType>,
ShellSort<CustomType>,
MergeSort<CustomType>,
OptimizedMergeSort<CustomType>,
QuickSort<CustomType>

...

我如何在 C++ 中使用 google 测试尽可能干净并尽可能多地重用?我迷失在类型化测试和类型参数化测试的丛林中 [1]:我什么时候应该使用其中之一?

谨致问候,

[1] https://github.com/google/googletest/blob/master/docs/advanced.md#type-parameterized-tests

令人沮丧的是,googletest API 到目前为止并没有给我们更多的 利用现代 C++ 使测试代码简洁,特别是对于测试 模板。但是直到 v1.8.x(当前版本系列位于 这个日期)googletest 一直致力于 C++98 兼容性,这主要是原因。 即将发布的版本 1.9.x 将转向 C++11 兼容性,我们希望 以获得更强大的 API.

尽管如此,现在可以编写相当简洁明了的 googletest 代码来 做你想做的事:也就是说,单元测试一致模板的不同值只有一个 模板参数。

实现它的方法不止一种。这是其中之一的工作示例, 使用 type-parameterized tests.

我们将有一组三个模板

template<typename T> struct (AA|BB|CC) {...};

每个都提供(至少)接口:

Name::Name(T const & u);
Name::operator int() const;
Name Name::operator+(Name const & u) const;
Name & Name::operator+=(Name const & u);
Name Name::operator-(Name const & u) const;
Name & Name::operator-=(Name const & u);

对于 Name = (AA|BB|CC)。我们想 对该接口进行单元测试,针对每个 (AA|BB|CC),每个实例化为 六种类型中的每一种:

char, int, float, AA<char>, BB<int>, CC<float>

所以这是要测试的 18 个实例化:

AA<char>, AA<int>, AA<float>, AA<AA<char>>, AA<BB<int>>, AA<CC<float>>
BB<char>, BB<int>, BB<float>, BB<AA<char>>, BB<BB<int>>, BB<CC<float>>
CC<char>, CC<int>, CC<float>, CC<AA<char>>, CC<BB<int>>, CC<CC<float>>

为了简短起见,我们将只实施两个通用测试。对于对象 任何实例化测试类型的 abc

  • a = b + c; b += c之后,然后是a == b
  • 给定 b != c,在 a = b - c; c -= b 之后,然后是 a != c

(至少,只要操作不溢出或 失去精度,我会避免这种情况。

所以我们预计总共会进行 36 次测试。

对于这个例子,我不关心 AABBCC 是什么,除了它们的通用接口之外, 所以我只是简单地从一个模型中导出它们,就像这样:

some_types.h

#pragma once

#include <type_traits>

namespace detail {
    template<typename T>
    struct bottom_type {
        using type = T;
    };

    template<template<typename ...> class C, typename ...Ts>
    struct bottom_type<C<Ts...>> {
        using type = typename C<Ts...>::type;
    };
}

template<typename T>
using bottom_t = typename detail::bottom_type<T>::type;

template<
    typename T,
    typename Enable = std::enable_if_t<std::is_arithmetic_v<bottom_t<T>>>
>
struct model
{
    using type = bottom_t<T>;

    model() = default;
    model(model const &) = default;
    model(T const & t)
    : _t{t}{}

    operator type() const { return _t; }

    auto operator+(model const & u) const {
        return _t + u;
    }

    auto & operator+=(model const & u) {
        _t += u;
        return *this;
    }

    auto operator-(model const & u ) const {
        return _t - u;
    }

    auto & operator-=(model const & u ) {
        _t -= u;
        return *this;
    }

protected:
    type _t = 0;
};

template<typename T> struct AA : model<T>{ using model<T>::model; };
template<typename T> struct BB : model<T>{ using model<T>::model; };
template<typename T> struct CC : model<T>{ using model<T>::model; };

现在这是我的 googletest 代码:

main.cpp

#include <array>
#include <algorithm>
#include <random>
#include <type_traits>
#include <limits>
#include <gtest/gtest.h>
#include "some_types.h"

template<typename T>
struct fixture : public ::testing::Test
{
protected:

    template<typename U>
    static auto const & test_data() {
        using type = bottom_t<U>;
        static std::array<type,1000> data;
        static bool called;
        if (!called) {
            std::default_random_engine gen;
            auto low = std::numeric_limits<type>::min() / 2;
            auto high = std::numeric_limits<type>::max() / 2;
            auto dist = [&low,&high](){
                if constexpr (std::is_floating_point_v<type>) {
                    return std::uniform_real_distribution<type>(low,high);
                } else {
                    return std::uniform_int_distribution<type>(low,high);
                }
            }();
            std::generate(
                data.begin(),data.end(),[&dist,&gen](){ return dist(gen); });
            called = true;
        }
        return data;
    }
};


template<template<typename> class C, typename ...Ts>
using test_types = ::testing::Types<C<Ts>...>;

using AA_test_types = test_types<AA,char,int,float,AA<char>,BB<int>,CC<float>>;
using BB_test_types = test_types<BB,char,int,float,AA<char>,BB<int>,CC<float>>;
using CC_test_types = test_types<CC,char,int,float,AA<char>,BB<int>,CC<float>>;

TYPED_TEST_SUITE_P(fixture);

TYPED_TEST_P(fixture, addition)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri)
    {
        TypeParam lhs{*fi}, rhs{*ri};
        auto sum = lhs + rhs;
        lhs += rhs;
        ASSERT_EQ(lhs,sum);
    }
}

TYPED_TEST_P(fixture, subtraction)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri) {
        TypeParam lhs{*fi}, rhs{*ri};
        if (lhs != rhs) {
            auto diff = lhs - rhs;
            rhs -= lhs;
            ASSERT_NE(rhs,diff);
        }
    }
}

REGISTER_TYPED_TEST_SUITE_P(fixture,addition,subtraction);
INSTANTIATE_TYPED_TEST_SUITE_P(AA_tests, fixture, AA_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(BB_tests, fixture, BB_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(CC_tests, fixture, CC_test_types);

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

让我们来看看兴趣点:-

template<template<typename> class C, typename ...Ts>
using test_types = ::testing::Types<C<Ts>...>;

在这里,我将 test_types 设为 ::testing::Types<SomeType...> 列表的模板别名 其中 SomeType 将是被测模板之一的实例化。作为它 碰巧,我的模板 AABBCC(和你的一样)都是以下形式:

template<typename T> class;

所以我希望 test_types 成为:

::testing::Types<C<Ts>...>

然后我定义了3个具体的类型别名:

using AA_test_types = test_types<AA,char,int,float,AA<char>,BB<int>,CC<float>>;
using BB_test_types = test_types<BB,char,int,float,AA<char>,BB<int>,CC<float>>;
using CC_test_types = test_types<CC,char,int,float,AA<char>,BB<int>,CC<float>>;

分别相当于:

::testing::Types<AA<char>, AA<int>, AA<float>, AA<AA<char>>, AA<BB<int>>, AA<CC<float>>>;
::testing::Types<BB<char>, BB<int>, BB<float>, BB<AA<char>>, BB<BB<int>>, BB<CC<float>>>;
::testing::Types<CC<char>, CC<int>, CC<float>, CC<AA<char>>, CC<BB<int>>, CC<CC<float>>>;

然后我使用模板夹具 fixture.

定义类型参数化测试套件
TYPED_TEST_SUITE_P(fixture);

然后我定义我的两个类型参数化测试模式。

TYPED_TEST_P(fixture, addition)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri)
    {
        TypeParam lhs{*fi}, rhs{*ri};
        auto sum = lhs + rhs;
        lhs += rhs;
        ASSERT_EQ(lhs,sum);
    }
}

TYPED_TEST_P(fixture, subtraction)
{
    using wrapped_type = typename TypeParam::type;
    auto const & data = this->template test_data<wrapped_type>();
    auto fi = data.begin(); auto ri = data.rbegin();
    for ( ; fi != ri.base(); ++fi, ++ri) {
        TypeParam lhs{*fi}, rhs{*ri};
        if (lhs != rhs) {
            auto diff = lhs - rhs;
            rhs -= lhs;
            ASSERT_NE(rhs,diff);
        }
    }
}

然后我在每次实例化时注册这两种模式以进行实例化 的 fixture

REGISTER_TYPED_TEST_SUITE_P(fixture,addition,subtraction);

然后我为 fixture 创建了 3 个名为 (AA|BB|CC)_tests 的实例 测试类型列表 (AA|BB|CC)_test_types 分别为:

INSTANTIATE_TYPED_TEST_SUITE_P(AA_tests, fixture, AA_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(BB_tests, fixture, BB_test_types);
INSTANTIATE_TYPED_TEST_SUITE_P(CC_tests, fixture, CC_test_types);

就是这样。编译并 link:

$ g++ -std=c++17 -Wall -Wextra -pedantic -o gtester main.cpp -lgtest -pthread

运行:

./gtester
[==========] Running 36 tests from 18 test suites.
[----------] Global test environment set-up.
[----------] 2 tests from AA_tests/fixture/0, where TypeParam = AA<char>
[ RUN      ] AA_tests/fixture/0.addition
[       OK ] AA_tests/fixture/0.addition (0 ms)
[ RUN      ] AA_tests/fixture/0.subtraction
[       OK ] AA_tests/fixture/0.subtraction (1 ms)
[----------] 2 tests from AA_tests/fixture/0 (1 ms total)

[----------] 2 tests from AA_tests/fixture/1, where TypeParam = AA<int>
[ RUN      ] AA_tests/fixture/1.addition
[       OK ] AA_tests/fixture/1.addition (0 ms)
[ RUN      ] AA_tests/fixture/1.subtraction
[       OK ] AA_tests/fixture/1.subtraction (0 ms)
[----------] 2 tests from AA_tests/fixture/1 (0 ms total)
...
...
...
[----------] 2 tests from CC_tests/fixture/4, where TypeParam = CC<BB<int> >
[ RUN      ] CC_tests/fixture/4.addition
[       OK ] CC_tests/fixture/4.addition (0 ms)
[ RUN      ] CC_tests/fixture/4.subtraction
[       OK ] CC_tests/fixture/4.subtraction (0 ms)
[----------] 2 tests from CC_tests/fixture/4 (0 ms total)

[----------] 2 tests from CC_tests/fixture/5, where TypeParam = CC<CC<float> >
[ RUN      ] CC_tests/fixture/5.addition
[       OK ] CC_tests/fixture/5.addition (0 ms)
[ RUN      ] CC_tests/fixture/5.subtraction
[       OK ] CC_tests/fixture/5.subtraction (0 ms)
[----------] 2 tests from CC_tests/fixture/5 (0 ms total)

[----------] Global test environment tear-down
[==========] 36 tests from 18 test suites ran. (4 ms total)
[  PASSED  ] 36 tests.