如果导入不起作用,我想跳过整个测试:
try:
import networkx
except:
import pytest
pytest.skip()
这适用于python-2.7和python-3.5,两者都使用pytest-2.8.1。当我尝试使用pytest-3.0.5在python-3.6中运行代码时,我收到以下错误:
不允许在测试之外使用pytest.skip。如果您正在尝试装饰测试功能,请改用@ pytest.mark.skip或@ pytest.mark.skipif装饰器。
如何编写适用于所有环境的代码/测试?我已经尝试重写像这样的except块,但它只适用于最新的配置:
try:
pytest.skip()
except:
pytestmark = pytest.mark.skip
答案 0 :(得分:6)
更直接的解决方案是使用pytest.importorskip代替。
答案 1 :(得分:1)
我自己想通了。跳过块需要检查pytest的版本:
if pytest.__version__ < "3.0.0":
pytest.skip()
else:
pytestmark = pytest.mark.skip