Throwin std::exception 导致 VS 2013 单元测试模块崩溃
Throwin std::exception causes VS 2013 unit testing module crash
我在 Visual Studio 2013 年为我的 C++ 应用程序创建了单元测试项目。不幸的是,它在测试期间没有正确处理异常:如果我 运行 测试只执行以下操作:
throw std::runtime_exception("hello!");
我看到 Windows "application has stopped working" 框告诉我 vstest.executionengine.exe
崩溃了。同时,visual studio 中的 Test Explorer 显示此测试已通过且没有错误。
Visual studio 本身报告 "active test" 已中止,错误是意外的。参考 https://msdn.microsoft.com/en-us/library/bb787181(VS.85).aspx 但该页面上绝对没有关于我的问题的有用信息。
如何在 C++ 中正确终止测试执行?
解决方案
- 确保您的所有测试都可以 运行 一次,因为这就是单元测试在 VS2013 中的工作方式(运行 我的意思是)
- 确保没有 assert(..) 调用,使用 header
CppUnitTestAssert.h
提供的 Assert
class
- 尝试从您的测试代码中删除 I/O,例如,尝试使用 header
CppUnitTestLogger.h
提供的 Logger
class 而不是您自己的实现
- 使用命令TESTS > Debug > Debug all tests to 运行 all tests in debug mode, 你会definetely find你崩溃的原因。
崩溃原因
我认为这就是断言在我的代码中的工作方式,也许是 abort()
。这导致托管过程出现意外情况。
我在 Visual Studio 2013 年为我的 C++ 应用程序创建了单元测试项目。不幸的是,它在测试期间没有正确处理异常:如果我 运行 测试只执行以下操作:
throw std::runtime_exception("hello!");
我看到 Windows "application has stopped working" 框告诉我 vstest.executionengine.exe
崩溃了。同时,visual studio 中的 Test Explorer 显示此测试已通过且没有错误。
Visual studio 本身报告 "active test" 已中止,错误是意外的。参考 https://msdn.microsoft.com/en-us/library/bb787181(VS.85).aspx 但该页面上绝对没有关于我的问题的有用信息。
如何在 C++ 中正确终止测试执行?
解决方案
- 确保您的所有测试都可以 运行 一次,因为这就是单元测试在 VS2013 中的工作方式(运行 我的意思是)
- 确保没有 assert(..) 调用,使用 header
CppUnitTestAssert.h
提供的Assert
class - 尝试从您的测试代码中删除 I/O,例如,尝试使用 header
CppUnitTestLogger.h
提供的Logger
class 而不是您自己的实现 - 使用命令TESTS > Debug > Debug all tests to 运行 all tests in debug mode, 你会definetely find你崩溃的原因。
崩溃原因
我认为这就是断言在我的代码中的工作方式,也许是 abort()
。这导致托管过程出现意外情况。