无意中从 gtest 中的不同测试套件合并的测试

Tests unintentionally merging from different test suites in gtest

我正在尝试为基本排序算法编写简单的 UnitTests。在使用 gtest 时,我 运行 遇到了这个奇怪的问题,来自不同套件的测试正在合并:

[==========] Running 20 tests from 2 test suites.
[----------] Global test environment set-up.
[----------] 10 tests from Suite_1/TestSortFunctions
[ RUN      ] Suite_1/TestSortFunctions.TestUnsortedArray/Bubble_Sort
[       OK ] Suite_1/TestSortFunctions.TestUnsortedArray/Bubble_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestUnsortedArray/Insertion_Sort
[       OK ] Suite_1/TestSortFunctions.TestUnsortedArray/Insertion_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestUnsortedArray/Merge_Sort
[       OK ] Suite_1/TestSortFunctions.TestUnsortedArray/Merge_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestUnsortedArray/Quick_Sort
[       OK ] Suite_1/TestSortFunctions.TestUnsortedArray/Quick_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestUnsortedArray/Selection_Sort
[       OK ] Suite_1/TestSortFunctions.TestUnsortedArray/Selection_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestEmptyArray/Bubble_Sort
[       OK ] Suite_1/TestSortFunctions.TestEmptyArray/Bubble_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestEmptyArray/Insertion_Sort
[       OK ] Suite_1/TestSortFunctions.TestEmptyArray/Insertion_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestEmptyArray/Merge_Sort
[       OK ] Suite_1/TestSortFunctions.TestEmptyArray/Merge_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestEmptyArray/Quick_Sort
[       OK ] Suite_1/TestSortFunctions.TestEmptyArray/Quick_Sort (0 ms)
[ RUN      ] Suite_1/TestSortFunctions.TestEmptyArray/Selection_Sort
[       OK ] Suite_1/TestSortFunctions.TestEmptyArray/Selection_Sort (0 ms)
[----------] 10 tests from Suite_1/TestSortFunctions (6 ms total)

[----------] 10 tests from Suite_2/TestSortFunctions
[ RUN      ] Suite_2/TestSortFunctions.TestUnsortedArray/Bubble_Sort
[       OK ] Suite_2/TestSortFunctions.TestUnsortedArray/Bubble_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestUnsortedArray/Insertion_Sort
[       OK ] Suite_2/TestSortFunctions.TestUnsortedArray/Insertion_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestUnsortedArray/Merge_Sort
[       OK ] Suite_2/TestSortFunctions.TestUnsortedArray/Merge_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestUnsortedArray/Quick_Sort
[       OK ] Suite_2/TestSortFunctions.TestUnsortedArray/Quick_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestUnsortedArray/Selection_Sort
[       OK ] Suite_2/TestSortFunctions.TestUnsortedArray/Selection_Sort (1 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestEmptyArray/Bubble_Sort
[       OK ] Suite_2/TestSortFunctions.TestEmptyArray/Bubble_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestEmptyArray/Insertion_Sort
[       OK ] Suite_2/TestSortFunctions.TestEmptyArray/Insertion_Sort (1 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestEmptyArray/Merge_Sort
[       OK ] Suite_2/TestSortFunctions.TestEmptyArray/Merge_Sort (0 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestEmptyArray/Quick_Sort
[       OK ] Suite_2/TestSortFunctions.TestEmptyArray/Quick_Sort (1 ms)
[ RUN      ] Suite_2/TestSortFunctions.TestEmptyArray/Selection_Sort
[       OK ] Suite_2/TestSortFunctions.TestEmptyArray/Selection_Sort (0 ms)
[----------] 10 tests from Suite_2/TestSortFunctions (4 ms total)

[----------] Global test environment tear-down
[==========] 20 tests from 2 test suites ran. (14 ms total)
[  PASSED  ] 20 tests.

我打算 Suite_1 测试只测试所有算法的 TestUnsortedArray 情况,Suite_2 再次测试所有算法的 TestEmptyArray 情况。排序函数看起来像简单的 C 构造,例如:void(*algorithm)(int *array, int length)。这是我的单元测试文件的代码:

#include <gtest/gtest.h>
#include <vector>
#include <functional>
#include "BubbleSort.h"
#include "InsertionSort.h"
#include "MergeSort.h"
#include "QuickSort.h"
#include "SelectionSort.h"

namespace UnitTests
{
    struct SortingAlgorithmTest : testing::Test
    {
        std::function<void(int *, int)> sorting_alg;
        std::vector<int> input_array;
        struct PrintToStringParamName
        {
            template <class ParamType>
            std::string operator()(const testing::TestParamInfo<ParamType>& info) const
            {
                auto param = static_cast<TestSortParam>(info.param);
                return param.name;
            }
        };
    };

    struct TestSortParam
    {
        const std::function<void(int *, int)> sorting_alg;
        const std::string name;
        const std::vector<int> test_array;
        const std::vector<int> expected_result;
    };

    struct TestSortFunctions : SortingAlgorithmTest, testing::WithParamInterface<TestSortParam>
    {
        TestSortFunctions()
        {
            sorting_alg = GetParam().sorting_alg;
            input_array = GetParam().test_array;
        }
    };

    TEST_P(TestSortFunctions, TestUnsortedArray)
    {
        sorting_alg(input_array.data(), static_cast<int>(input_array.size()));
        EXPECT_EQ(input_array, GetParam().expected_result);
    }

    INSTANTIATE_TEST_SUITE_P(Suite_1, TestSortFunctions,
        testing::Values(
            TestSortParam{  bubble_sort,
                            "Bubble_Sort",
                            { 1, 5, 4, 0, 7, 2, 9, 3 },
                            { 0, 1, 2, 3, 4, 5, 7, 9 }
                        },
            TestSortParam{  insertion_sort,
                            "Insertion_Sort",
                            { 1, 5, 4, 0, 7, 2, 9, 3 },
                            { 0, 1, 2, 3, 4, 5, 7, 9 }
                        },
            TestSortParam{  merge_sort,
                            "Merge_Sort",
                            { 1, 5, 4, 0, 7, 2, 9, 3 },
                            { 0, 1, 2, 3, 4, 5, 7, 9 }
                        },
            TestSortParam{  quick_sort,
                            "Quick_Sort",
                            { 1, 5, 4, 0, 7, 2, 9, 3 },
                            { 0, 1, 2, 3, 4, 5, 7, 9 }
                        },
            TestSortParam{  selection_sort,
                            "Selection_Sort",
                            { 1, 5, 4, 0, 7, 2, 9, 3 },
                            { 0, 1, 2, 3, 4, 5, 7, 9 }
                        }
        ), SortingAlgorithmTest::PrintToStringParamName()
    );

    TEST_P(TestSortFunctions, TestEmptyArray)
    {
        sorting_alg(input_array.data(), static_cast<int>(input_array.size()));
        EXPECT_EQ(input_array, GetParam().expected_result);
    }

    INSTANTIATE_TEST_SUITE_P(Suite_2, TestSortFunctions,
        testing::Values(
            TestSortParam{  bubble_sort,
                            "Bubble_Sort",
                            {},
                            {}
                        },
            TestSortParam{  insertion_sort,
                            "Insertion_Sort",
                            {},
                            {}
                        },
            TestSortParam{  merge_sort,
                            "Merge_Sort",
                            {},
                            {}
                        },
            TestSortParam{  quick_sort,
                            "Quick_Sort",
                            {},
                            {}
                        },
            TestSortParam{  selection_sort,
                            "Selection_Sort",
                            {},
                            {}
                        }
        ), SortingAlgorithmTest::PrintToStringParamName()
    );
}

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

谁能帮我找出错误?另外,我稍后会考虑将测试数组移到一个地方,并且我想 运行 对所有算法使用相同的相应参数进行测试。将这些参数集移动到什么地方才是正确的?谢谢!

这是 GoogleTest 的设计功能 - 来自 TestSortFunctions 的所有 TEST_P 测试都将 运行 以及来自 INSTANTIATE_TEST_SUITE_P 的所有可能的实例化,根据文档 [=13] =] here.

在您的情况下,只需创建两个单独的测试套件:从 TestSortFunctions 派生并创建 TestSortFunctionsEmptyInputsTestSortFunctionsNonEmpty,如果您确实需要,则使用这些测试套件调用 INSTANTIATE_TEST_SUITE_P到。但是,我认为有一个 TestSortFunctions 和一个 TEST_P 测试是完全可以的,用 Suite_1Suite_2 实例化。毕竟这些都属于你要测试的同一个功能