Python Unittest:断言正确的系统退出代码
我使用断言我的脚本将引发正确的Python Unittest:断言正确的系统退出代码,python,unit-testing,Python,Unit Testing,我使用断言我的脚本将引发正确的SystemExitcode 基于 我把这个编码了: with self.assertRaises(SystemExit) as cm: do_something() the_exception = cm.exception self.assertEqual(the_exception.error_code, 3) 然而,这是行不通的。出现以下错误: AttributeError: 'SystemExit' object has no attribute
SystemExit
code
基于
我把这个编码了:
with self.assertRaises(SystemExit) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.error_code, 3)
然而,这是行不通的。出现以下错误:
AttributeError: 'SystemExit' object has no attribute 'error_code'
直接从BaseException派生,而不是StandardError,因此它没有属性error\u code
您必须使用属性code
,而不是error\u code
。示例如下所示:
with self.assertRaises(SystemExit) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.code, 3)
另一种可能的重复:另一个问题是重复的,因为它是在2013年发布的,这个问题是在2012年提出和回答的
with self.assertRaises(SystemExit) as cm:
do_something()
the_exception = cm.exception
self.assertEqual(the_exception.code, 3)