如果导入不起作用,我想跳过整个测试:
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装饰器。
如何编写在所有提到的环境中工作的代码/测试?我已经尝试过像这样重写but块,但是它只适用于最新的配置:
try:
pytest.skip()
except:
pytestmark = pytest.mark.skip发布于 2017-02-28 23:14:19
一个更简单的解决方案是使用pytest.importorskip。
发布于 2017-02-28 15:03:11
我自己想出来的。跳过块需要检查pytest的版本:
if pytest.__version__ < "3.0.0":
pytest.skip()
else:
pytestmark = pytest.mark.skip发布于 2022-02-25 11:08:33
可以使用pytest.skip和allow_module_level=True跳过模块的其余测试:
pytest.skip(allow_module_level=True)参见:https://docs.pytest.org/en/latest/how-to/skipping.html#skipping-test-functions中的示例
https://stackoverflow.com/questions/42511879
复制相似问题