当运行调用
os.fork
然后调用 sys.exit
的测试用例时,pytest
将捕获新进程中产生的 SystemExit
异常,并将相关测试用例标记为失败:
> sys.exit(0)
E SystemExit: 0
这种使用可能是有意的。如何以一种不会导致
pytest
将其标记为失败的方式允许它?
import functools
import os
def allow_fork_exit(function):
@functools.wraps(function)
def wrapped(*args, **kwargs):
mainpid = os.getpid()
try:
return function(*args, **kwargs)
except SystemExit as sysexit:
if sysexit.code or os.getpid() == mainpid:
raise
# If pytest sees a SystemExit, it'll mark the test as failure.
return
return wrapped
有更好的解决办法吗?
除了 stackoverflow 强制的 CC BY-SA 4.0 许可证之外,您还可以在 MIT 下使用它。