Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Unittest:断言正确的系统退出代码_Python_Unit Testing - Fatal编程技术网

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)