Pytest下的Python测试运行没有正确捕获CalledProcessError异常
Python test run under Pytest does not catch CalledProcessError exceptions correctly
当 运行 Python 在 Pytest (py.test
) 中进行 doctests 时,我现在正面临一个奇怪的行为。以下片段突出显示了行为。
- 当通过
python2.7 ./weirdpytestbehaviour_test.py
. 调用脚本时,文档测试 会通过
- 通过
py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short
. 调用脚本时,文档测试 未通过
- 如果我将语句
except CalledProcessError as e:
更改为 except Exception as e:
那么 doctests 也会通过 py.test
.
似乎 py.test 对 CalledProcessError
异常类型有一些特殊处理。代码在 py.test
中执行时采用不同的评估路径,这是非常不直观的。
我如何控制或调整该行为?
#!/usr/bin/python
# coding=utf-8
# file: 'weirdpytestbehaviour_test.py'
from subprocess import check_output, CalledProcessError
def func(success):
"""The worker function.
>>> func(success=True)
>>> func(success=False)
Ouch!
Special treatment of this particular failure...
"""
try:
# Now invoke a subprocess
if success:
# Will always succeed and return with an exit code of 0
check_output('echo "Hello!"; exit 0', shell=True)
else:
# Will always succeed and return with an exit code of 1
check_output('echo "Ouch!"; exit 1', shell=True)
# Does not catch the exception under pytest
# 'except Exception as e:' would work as expected
except CalledProcessError as e:
print(e.output),
if "Ouch!" in e.output:
print("Special treatment of this particular failure...")
else:
raise
if __name__ == '__main__':
import doctest
doctest.testmod()
失败情况下py.test的输出(试图捕获上面的CalledProcessError)如下:
$ py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short
Test session starts (platform: linux2, Python 2.7.6, pytest 3.4.2, pytest-sugar 0.9.1)
[...]
007 The worker function.
008
009 >>> func(success=True)
010
011 >>> func(success=False)
UNEXPECTED EXCEPTION: CalledProcessError()
Traceback (most recent call last):
File "/usr/lib/python2.7/doctest.py", line 1315, in __run
compileflags, 1) in test.globs
File "<doctest experiments.weirdpytestbehaviour_test.func[1]>", line 1, in <module>
File "weirdpytestbehaviour_test.py", line 22, in func
check_output('echo "Ouch!"; exit 1', shell=True)
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command 'echo "Ouch!"; exit 1' returned non-zero exit status 1
weirdpytestbehaviour_test.py:11: UnexpectedException
weirdpytestbehaviour_test.py ⨯ 100% ██████████
Results (0.19s):
1 failed
- weirdpytestbehaviour_test.py:11 [doctest] weirdpytestbehaviour_test.func
经过反复试验,我通过卸载解决了这个问题,re-installing 不仅是 pytest,还有我使用的插件。之后它又开始工作了。
清理缓存(__pycache__
)没有效果。显然pytest及其插件的安装有问题。
当 运行 Python 在 Pytest (py.test
) 中进行 doctests 时,我现在正面临一个奇怪的行为。以下片段突出显示了行为。
- 当通过
python2.7 ./weirdpytestbehaviour_test.py
. 调用脚本时,文档测试 会通过
- 通过
py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short
. 调用脚本时,文档测试 未通过
- 如果我将语句
except CalledProcessError as e:
更改为except Exception as e:
那么 doctests 也会通过py.test
.
似乎 py.test 对 CalledProcessError
异常类型有一些特殊处理。代码在 py.test
中执行时采用不同的评估路径,这是非常不直观的。
我如何控制或调整该行为?
#!/usr/bin/python
# coding=utf-8
# file: 'weirdpytestbehaviour_test.py'
from subprocess import check_output, CalledProcessError
def func(success):
"""The worker function.
>>> func(success=True)
>>> func(success=False)
Ouch!
Special treatment of this particular failure...
"""
try:
# Now invoke a subprocess
if success:
# Will always succeed and return with an exit code of 0
check_output('echo "Hello!"; exit 0', shell=True)
else:
# Will always succeed and return with an exit code of 1
check_output('echo "Ouch!"; exit 1', shell=True)
# Does not catch the exception under pytest
# 'except Exception as e:' would work as expected
except CalledProcessError as e:
print(e.output),
if "Ouch!" in e.output:
print("Special treatment of this particular failure...")
else:
raise
if __name__ == '__main__':
import doctest
doctest.testmod()
失败情况下py.test的输出(试图捕获上面的CalledProcessError)如下:
$ py.test ./weirdpytestbehaviour_test.py --doctest-modules --tb=short
Test session starts (platform: linux2, Python 2.7.6, pytest 3.4.2, pytest-sugar 0.9.1)
[...]
007 The worker function.
008
009 >>> func(success=True)
010
011 >>> func(success=False)
UNEXPECTED EXCEPTION: CalledProcessError()
Traceback (most recent call last):
File "/usr/lib/python2.7/doctest.py", line 1315, in __run
compileflags, 1) in test.globs
File "<doctest experiments.weirdpytestbehaviour_test.func[1]>", line 1, in <module>
File "weirdpytestbehaviour_test.py", line 22, in func
check_output('echo "Ouch!"; exit 1', shell=True)
File "/usr/lib/python2.7/subprocess.py", line 573, in check_output
raise CalledProcessError(retcode, cmd, output=output)
CalledProcessError: Command 'echo "Ouch!"; exit 1' returned non-zero exit status 1
weirdpytestbehaviour_test.py:11: UnexpectedException
weirdpytestbehaviour_test.py ⨯ 100% ██████████
Results (0.19s):
1 failed
- weirdpytestbehaviour_test.py:11 [doctest] weirdpytestbehaviour_test.func
经过反复试验,我通过卸载解决了这个问题,re-installing 不仅是 pytest,还有我使用的插件。之后它又开始工作了。
清理缓存(__pycache__
)没有效果。显然pytest及其插件的安装有问题。