如何绕过“sys.exit()”在 python 鼻子测试中?
看来 python nostest 在遇到 sys.exit() 时会退出,并且对此内置函数的模拟不起作用。
It seems that python nosetest will quit when encountered sys.exit()
, and mocking of this builtin doesn't work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以尝试捕获 SystemExit 异常。当有人调用 sys.exit() 时会引发该异常。
You can try catching the SystemExit exception. It is raised when someone calls
sys.exit()
.如果您使用
mock
来修补sys.exit
,则可能会错误地修补它。这个小测试对我来说效果很好:
调用方式:
输出:
If you're using
mock
to patchsys.exit
, you may be patching it incorrectly.This small test works fine for me:
invoked with:
outputs:
请记住,程序可能合理地期望在
sys.exit()
之后不再继续,因此修补它可能实际上没有帮助......Keep in mind that programs may reasonably expect not to continue after
sys.exit()
, so patching it out might not actually help...这是
unittest
框架中的一个示例。This is an example in the
unittest
framework.从这篇关于 medium 的文章中:
From this article on medium: