小编Der*_*per的帖子

如何在 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)
{
    // …
Run Code Online (Sandbox Code Playgroud)

c++ googletest

5
推荐指数
1
解决办法
1058
查看次数

如何对依赖Asio的一段代码进行单元测试?

我有一个包含 Asio 的类。它的目的是模拟域和 TCP 套接字上的通信,但我对自动化单元测试感到困惑。我查看了FakeIt,但它只测试虚拟方法,GoogleMocks建议对我的代码进行模板化,以便我可以通过单元测试的 MockAsio 实现和生产中的真实 Asio。

还有其他方法对网络代码进行单元测试吗?伪造域和 TCP 套接字而不是运行整个堆栈?如果我使用 GoogleMock,为什么要使用一个使用 GoogleMock 的类而不是我自己的实现来满足我的需要?

c++ sockets unit-testing network-programming boost-asio

4
推荐指数
1
解决办法
3227
查看次数