使用 CPPUnit 从异常中恢复

发布于 2024-07-07 11:11:53 字数 254 浏览 7 评论 0原文

我一直使用 CPPUnit 作为单元测试框架,现在尝试在自动构建和打包系统中使用它。 然而,一个阻碍我的问题是,如果在单元测试运行期间发生崩溃,例如空指针取消引用,它会停止其余的自动化。

有没有办法让CPPUnit从异常中恢复,记录测试失败然后优雅地存在而不是终止单元测试过程? 即使是专门针对空指针取消引用的方法也会很有用,因为我所遇到的问题中大约有 90% 都是这种方法。

为了特定于技术,我在 Windows 系统上使用 makefile。

I have been using CPPUnit as a unit testing framework and am now trying to use it in an automated build and package system. However a problem holding me back is that if a crash occurs during the running of the unit tests, e.g. a null pointer dereferencing, it halts the remainder of the automation.

Is there any way for CPPUnit to recover from the exception, record the test failure and then exist gracefully rather than terminating the unit test process? Even an approach specific to null pointer dereferencing would be useful as that makes up about 90% of the issues I have had.

To be technology-specific, I am using makefiles on a Windows system.

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

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

发布评论

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

评论(5

叫思念不要吵 2024-07-14 11:11:53

您正在构建过程中自动执行基于 cppunit 的单元测试,对吗?

如果您尝试使用 CppUnit 执行构建过程,我会想说不要这样做!

您能告诉我们当单元测试崩溃时是什么停止了构建过程吗? 您的单元测试是由什么启动的,Makefile、您自己的脚本还是持续集成框架


为了尝试回答您的问题,CppUnit 无法从违规或分段错误中恢复。 在类 Unix 系统上,您应该能够捕获 SIGSEGV 并继续,但是处于哪种状态

如果崩溃发生在单元测试中而不是产品中,那么我建议您依赖 断言保护以防止取消引用 NULL 指针:

class TestObject : public CPPUNIT_NS::TestCase
{
  CPPUNIT_TEST_SUITE(Test);
  CPPUNIT_TEST(testObjectIsReady);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp(void) {}
  void tearDown(void) {} 

protected:
  void testObjectIsReady(void)
  { 
     Object *theObject = GetObject();

     CPPUNIT_ASSERT_MESSAGE("check pointer is not null", theObject != NULL);

     //--- now you can play with your object without dereferencing a NULL pointer
     CPPUNIT_ASSERT_MESSAGE("check objet is ready", theObject->isReady());
  }
};

You're automating the execution of your cppunit-based unit-tests during your build process, right ?

If you were trying to use CppUnit to execute the build process, I would be tempted to say don't do that !

Could you tell us what is stopping the build process when the unit tests crash ? And what are your unit tests started by, a Makefile, a script of your own, or a continuous integration framework ?


To try to answer your question, CppUnit cannot recover from violation or segmentation errors. On Unix-like systems you should be able to catch the SIGSEGV and to continue, but in which state ?

If your crashes occur in your unit test and not in your product, then I'd recommend you to rely on assertion guards to prevent dereferencing NULL pointers:

class TestObject : public CPPUNIT_NS::TestCase
{
  CPPUNIT_TEST_SUITE(Test);
  CPPUNIT_TEST(testObjectIsReady);
  CPPUNIT_TEST_SUITE_END();

public:
  void setUp(void) {}
  void tearDown(void) {} 

protected:
  void testObjectIsReady(void)
  { 
     Object *theObject = GetObject();

     CPPUNIT_ASSERT_MESSAGE("check pointer is not null", theObject != NULL);

     //--- now you can play with your object without dereferencing a NULL pointer
     CPPUNIT_ASSERT_MESSAGE("check objet is ready", theObject->isReady());
  }
};
情痴 2024-07-14 11:11:53

很抱歉这么说,但您之前收到的有关此问题的答案很荒谬。
cppunit在这方面确实欠缺。 cppunit 应该实现一个 EXIT_ON_FAIL 宏,它允许您捕获 Windows 中的访问冲突(使用 SetUnhandledExceptionFilter),然后您可以进行任何清理并允许 cpp-unit 通过 EXIT_ON_FAIL 报告失败。 然后报告后,退出应用程序。

Sorry to say this but the previous answers you received on this are ridiculous.
cppunit really lacks in this regard. cppunit should implement an EXIT_ON_FAIL macro which allows you to trap the access violation in windows (using SetUnhandledExceptionFilter), then you can do any clean-up and allow cpp-unit to report the failure via EXIT_ON_FAIL. Then after reporting, exit the application.

夜吻♂芭芘 2024-07-14 11:11:53

在 C/C++ 中,从此类错误中恢复的最佳方法是在单独的进程中运行每个测试,然后从父进程监视它们。 这在 UNIX 中非常简单——只需在测试开始之前 fork() 即可。 check 支持此功能,您可以轻松地修补 CPPUnit 以实现此行为。

In C/C++, the best way to recover from errors like that is to run each test in a separate process and then monitor them from a parent process. This is very easy in UNIX -- just fork() before the test begins. check supports this, and you could likely patch CPPUnit to have this behavior without much fuss.

温暖的光 2024-07-14 11:11:53

作为稍后仔细研究此问题的任何人的附加说明,我发现 UnitTest++ 可以捕获测试中出现异常,并且只是通过适当的信息使测试失败,而不是导致进程退出。

As an additional note to anyone perusing this question later, I've found UnitTest++ can catch exceptions in tests and just fail the test with appropriate information rather than resulting in a process exit.

ゝ杯具 2024-07-14 11:11:53

我没有尝试过,但如果在 Windows 中,我想使用 SEH 会有所帮助:

__try
{
// running your case
}

__except
{
}

将其集成到 CppUnit 框架中,每次收到未知异常时,将案例标记为失败。

I didn't try it, but if in Windows, I guess use SEH would help:

__try
{
// running your case
}

__except
{
}

Integrate it into the CppUnit framework, and everytime receive an unknown exception, mark the case as fail.

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