测试无意中从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)。这是我的UnitTests文件的代码:

#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的设计功能-根据此处的文档Please note that INSTANTIATE_TEST_SUITE_P will instantiate all tests in the given test suite, whether their definitions come before or after the INSTANTIATE_TEST_SUITE_P statement.,来自TestSortFunctions的所有TEST_P测试都将使用来自INSTANTIATE_TEST_SUITE_P的所有可能的实例化来运行。

在您的情况下,只需创建两个独立的测试套件:从TestSortFunctions派生并创建TestSortFunctionsEmptyInputsTestSortFunctionsNonEmpty,如果您真的需要,还可以使用这些测试套件调用INSTANTIATE_TEST_SUITE_P。然而,我认为使用Suite_1Suite_2实例化一个TestSortFunctions和一个TEST_P测试是完全可以的。毕竟,所有这些都属于您想要测试的相同函数。

最新更新