本文介绍了Googletest参数化测试崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我刚刚了解了 googletest中的值参数化单元测试 标题: #include< gtest / gtest.h> namespace EnsembleClustering { class ParametrizedGTest:public testing :: TestWithParam< int> { public: ParametrizedGTest(); virtual〜ParametrizedGTest(); }; } / * namespace EnsembleClustering * / #includeParametrizedGTest.h namespace EnsembleClustering { ParametrizedGTest :: ParametrizedGTest (){ // TODO自动生成的构造函数stub } ParametrizedGTest ::〜ParametrizedGTest(){ // TODO自动生成destructor stub } TEST_P(ParametriizedGTest,testParameter){ int n = GetParam(); EXPECT_EQ(n,getParam()); } INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance, ParametrizedGTest, :: testing :: Values(100)); } / * namespace EnsembleClustering * / 像往常一样,程序崩溃,没有任何输出。 gdb堆栈跟踪是 EnsembleClustering-D [C / C ++应用程序] EnsembleClustering Thread [1 ](Suspended:信号:EXC_BAD_ACCESS:无法访问内存) __gnu_debug :: _ Safe_sequence_base :: _ M_attach_single()在0x100528add __gnu_debug :: _ Safe_sequence_base :: _ M_attach()在0x100528a74 __gnu_debug :: _Safe_iterator_base :: _M_attach()在0x100528bfe __gnu_debug :: _ Safe_iterator_base :: _ safe_iterator_base()at safe_base.h:90 0x1000016e9 __gnu_debug :: _ Safe_iterator< __ gnu_cxx :: __ normal_iterator< testing :: internal :: ParameterizedTestCaseInfoBase * *,std :: __ cxx1998 :: vector< testing :: internal :: ParameterizedTestCaseInfoBase *,std :: allocator< tests :: internal :: ParameterizedTestCaseInfoBase *> > >,std :: __ debug :: vector< testing :: internal :: ParameterizedTestCaseInfoBase *,std :: allocator< testing :: internal :: ParameterizedTestCaseInfoBase *> > > :: __ Safe_iterator()at safe_iterator.h:154 0x100002e9c std :: __ debug :: vector< testing :: internal :: ParameterizedTestCaseInfoBase *,std :: allocator< testing :: internal :: ParameterizedTestCaseInfoBase *> > :: begin()at vector:207 0x100001fbe testing :: internal :: ParameterizedTestCaseRegistry :: GetTestCasePatternHolder< EnsembleClustering :: ParametrizedGTest>()at gtest-param-util.h:574 0x1000025b0 EnsembleClustering :: ParametrizedGTest_testParameter_Test :: AddToRegistry()在ParametrizedGTest.cpp:22 0x100001d3f __static_initialization_and_destruction_0()在ParametrizedGTest.cpp:22 0x100001349 _GLOBAL__sub_I_ParametrizedGTest.cpp()在ParametrizedGTest.cpp:32 0x100001424 < ; ... more frames ...> gdb 我做错了什么或者这是googletest中的错误?您可以重现此错误吗? 编辑:我在Mac OS X 10.8。解决方案从查看gtest的源代码的唯一情况下,如果没有参数化测试可用是在Windows上使用VC7.1与禁用的异常: //我们不支持MSVC 7.1,现在禁用例外。因此 //我们关心的所有编译器都足以支持 //值参数化测试。 #define GTEST_HAS_PARAM_TEST 1 因此,您需要检查您的MinGW的构建并可能更新它? 有关MinGW的更多信息: 在他们的常见问题他们报告,当使用MinGW时,需要以下编译选项来构建gtest: PATH / TO / configure CC =gcc -mno-cygwinCXX = g ++ -mno-cygwin。 完成示例: #include< gtest / gtest.h> namespace EnsembleClustering { class ParametrizedGTest:public testing :: TestWithParam< int> { public: ParametrizedGTest(); virtual〜ParametrizedGTest(); }; ParametrizedGTest :: ParametrizedGTest(){} ParametrizedGTest ::〜ParametrizedGTest(){} TEST_P (ParametrizedGTest,testParameter){ int n = GetParam(); EXPECT_EQ(n,GetParam()); } INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance, ParametrizedGTest, :: testing :: Values(100)); } / * namespace EnsembleClustering * / int main(int argc,char * argv []){ :: testing :: InitGoogleTest (& argc,argv); return RUN_ALL_TESTS(); } 我在Mac OS X 10.8上使用以下编译器调用编译此代码: / p> g ++ -IGTEST_INCLUDE_DIR -LGTEST_LIB_DIR -lgtest -o tt2 tt2.cpp 其中 GTEST_INCLUDE_DIR 和 GTEST_LIB_DIR 并存储库文件。当你编译和执行时,会发生什么? I've just learned about value-parametrized unit tests in googletest and would like to use them in my project.I wrote a simple parametrized test.Header:#include <gtest/gtest.h>namespace EnsembleClustering {class ParametrizedGTest: public testing::TestWithParam<int> {public: ParametrizedGTest(); virtual ~ParametrizedGTest();};} /* namespace EnsembleClustering */Source:#include "ParametrizedGTest.h"namespace EnsembleClustering {ParametrizedGTest::ParametrizedGTest() { // TODO Auto-generated constructor stub}ParametrizedGTest::~ParametrizedGTest() { // TODO Auto-generated destructor stub}TEST_P(ParametrizedGTest, testParameter) { int n = GetParam(); EXPECT_EQ(n, GetParam());}INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance, ParametrizedGTest, ::testing::Values(100));} /* namespace EnsembleClustering */Now, when I run googletest as usual, the program crashes without any output. The gdb stack trace isEnsembleClustering-D [C/C++ Application] EnsembleClustering Thread [1] (Suspended : Signal : EXC_BAD_ACCESS:Could not access memory) __gnu_debug::_Safe_sequence_base::_M_attach_single() at 0x100528add __gnu_debug::_Safe_sequence_base::_M_attach() at 0x100528a74 __gnu_debug::_Safe_iterator_base::_M_attach() at 0x100528bfe __gnu_debug::_Safe_iterator_base::_Safe_iterator_base() at safe_base.h:90 0x1000016e9 __gnu_debug::_Safe_iterator<__gnu_cxx::__normal_iterator<testing::internal::ParameterizedTestCaseInfoBase**, std::__cxx1998::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> > >, std::__debug::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> > >::_Safe_iterator() at safe_iterator.h:154 0x100002e9c std::__debug::vector<testing::internal::ParameterizedTestCaseInfoBase*, std::allocator<testing::internal::ParameterizedTestCaseInfoBase*> >::begin() at vector:207 0x100001fbe testing::internal::ParameterizedTestCaseRegistry::GetTestCasePatternHolder<EnsembleClustering::ParametrizedGTest>() at gtest-param-util.h:574 0x1000025b0 EnsembleClustering::ParametrizedGTest_testParameter_Test::AddToRegistry() at ParametrizedGTest.cpp:22 0x100001d3f __static_initialization_and_destruction_0() at ParametrizedGTest.cpp:22 0x100001349 _GLOBAL__sub_I_ParametrizedGTest.cpp() at ParametrizedGTest.cpp:32 0x100001424 <...more frames...> gdbAm I doing something wrong or is this a bug in googletest? Can you reproduce this error?EDIT: I am on Mac OS X 10.8. 解决方案 From looking at the source code of gtest the only case if there are no parametrized tests available is on Windows using VC7.1 with disabled exceptions:// We don't support MSVC 7.1 with exceptions disabled now. Therefore// all the compilers we care about are adequate for supporting// value-parameterized tests.#define GTEST_HAS_PARAM_TEST 1So, you'll need to check how your MinGW was built and probably update it? And can you run the gtest unit tests to see if they execute the typed parameters test?More information on MinGW:On their FAQ they report that when using MinGW the following compile option for building gtest is required: PATH/TO/configure CC="gcc -mno-cygwin" CXX="g++ -mno-cygwin".Complete Example:#include <gtest/gtest.h>namespace EnsembleClustering { class ParametrizedGTest: public testing::TestWithParam<int> { public: ParametrizedGTest(); virtual ~ParametrizedGTest(); }; ParametrizedGTest::ParametrizedGTest() { } ParametrizedGTest::~ParametrizedGTest() { } TEST_P(ParametrizedGTest, testParameter) { int n = GetParam(); EXPECT_EQ(n, GetParam()); } INSTANTIATE_TEST_CASE_P(ParametrizedGTestInstance, ParametrizedGTest, ::testing::Values(100));} /* namespace EnsembleClustering */int main(int argc, char* argv[]) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS();}I compiled this code using the following compiler call on Mac OS X 10.8:g++ -IGTEST_INCLUDE_DIR -LGTEST_LIB_DIR -lgtest -o tt2 tt2.cppWhere GTEST_INCLUDE_DIR and GTEST_LIB_DIR are the path where header and library files are stored. When you compile and execute, what happens? 这篇关于Googletest参数化测试崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-15 06:25