CppUnit 泄漏

发布于 2024-08-08 21:38:06 字数 2321 浏览 4 评论 0 原文

使用 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") );

如何取消注册这些测试?

running my regression tests with valgrind I have this kind of report:

==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)

I guess this is due the fact that Tests added to Suite are not removed before the main is over.

This is the way I register the test:

  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") );

How do I unregister those tests ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长安忆 2024-08-15 21:38:06

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 giving runner.addTest only part of your myTest instance, you're not providing any way for the entire myTest instance to get cleaned up on deletion. Manually delete'ing myTest after running probably won't work either, since runner will also try to delete the portion of myTest 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文