CppUnit 泄漏
使用 valgrind 运行我的回归测试我有这样的报告:
==20341== 256 bytes in 1 blocks are indirectly lost in loss record 915 of 919 ==20341== at 0x4A0661C: operator new(unsigned long) (vg_replace_malloc.c:220) ==20341== by 0x7F366FA: std::vector<CppUnit::Test*, std::allocator<CppUnit::Test*> >::_M_insert_aux(__gnu_cxx::__normal_iterator<CppUnit::Test**, std::vector<CppUnit::Test*, std::allocator<CppUnit::Test*> > >, CppUnit::Test* const&) (new_allocator.h:88) ==20341== by 0x7F36496: CppUnit::TestSuite::addTest(CppUnit::Test*) (stl_vector.h:610) ==20341== by 0x585B80: TestVectorAlgebra::addTestsToSuite(CppUnit::TestSuiteBuilderContextBase&) (testvectoralgebra.h:30) ==20341== by 0x586719: TestVectorAlgebra::suite() (testvectoralgebra.h:42) ==20341== by 0x5948C4: CppUnit::TestSuiteFactory<TestVectorAlgebra>::makeTest() (TestSuiteFactory.h:20) ==20341== by 0x7F2C6B0: CppUnit::TestFactoryRegistry::addTestToSuite(CppUnit::TestSuite*) (TestFactoryRegistry.cpp:149) ==20341== by 0x7F2CAD5: CppUnit::TestFactoryRegistry::makeTest() (TestFactoryRegistry.cpp:136) ==20341== by 0x580760: main (testunit.cpp:88)
我想这是因为添加到套件中的测试在主程序结束之前没有被删除。
这是我注册测试的方式:
CppUnit::TextTestRunner::TestRunner runner;
// Get the top level suite from the registry
CppUnit::Test* myTest =
CPPUNIT_NS::TestFactoryRegistry::getRegistry().makeTest();
runner.addTest( myTest->findTest("TestVectorAlgebra") );
如何取消注册这些测试?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CppUnit 文档 建议
runner.addTest< /code> 拥有对其给出的任何测试的所有权。通过只提供
runner.addTest
myTest 实例的一部分,您并没有提供任何方法让整个 myTest 实例在删除时得到清理。运行后手动删除
可能也不起作用,因为runner
也会尝试删除给定的myTest
部分。如果您只想运行特定测试或测试子集,则应该尝试使用 testName 参数.6/class_text_test_runner.html#a2" rel="nofollow noreferrer">TextRunner::run。
(如果您有时间和意愿,您可能想研究不同的单元测试框架。 UnitTest++ 和 Google Test 比 CppUnit 更新、更易于使用且功能更丰富。)
The CppUnit documentation suggests that
runner.addTest
takes ownership of whatever test it's given. By givingrunner.addTest
only part of your myTest instance, you're not providing any way for the entire myTest instance to get cleaned up on deletion. Manuallydelete
'ing myTest after running probably won't work either, sincerunner
will also try to delete the portion ofmyTest
that it's been given.If you're interested in only running a particular test or subset of tests, you should instead try using the
testName
parameter of TextRunner::run.(And if you have the time and inclination, you might want to look into a different unit test framework. UnitTest++ and Google Test are newer, easier to use, and more featureful than CppUnit.)