擅长:python、mysql、java
<p>问题是,如果参数的类型转换器引发异常<code>ArgumentTypeError</code><code>agrparse</code><a href="https://docs.python.org/3/library/argparse.html#invalid-arguments" rel="nofollow noreferrer">exits</a>,则退出意味着引发内置异常<code>SystemExit</code>。因此,您必须捕获该异常并验证原始异常的类型是否正确:</p>
<pre><code>def test_non_existing_config_file():
try:
main([' configFile', 'non_existing_config_file.json'])
except SystemExit as e:
assert isinstance(e.__context__, argparse.ArgumentError)
else:
raise ValueError("Exception not raised")
</code></pre>