Pytest 有没有办法跳过测试夹具?
问题是我给定的fixture函数具有外部依赖性,这导致了“错误”(如无法访问网络/资源不足等) 我想跳过这个夹具,然后跳过任何依赖于这个夹具的测试 这样做是行不通的:Pytest 有没有办法跳过测试夹具?,pytest,Pytest,问题是我给定的fixture函数具有外部依赖性,这导致了“错误”(如无法访问网络/资源不足等) 我想跳过这个夹具,然后跳过任何依赖于这个夹具的测试 这样做是行不通的: import pytest @pytest.mark.skip(reason="Something.") @pytest.fixture(scope="module") def parametrized_username(): raise Exception("foobar") return 'overridde
import pytest
@pytest.mark.skip(reason="Something.")
@pytest.fixture(scope="module")
def parametrized_username():
raise Exception("foobar")
return 'overridden-username'
这将导致
_______________________________ ERROR at setup of test_username _______________________________
@pytest.mark.skip(reason="Something.")
@pytest.fixture(scope="module")
def parametrized_username():
> raise Exception("foobar")
E Exception: foobar
a2.py:6: Exception
跳过pytest夹具的正确方法是什么?是的,您可以轻松做到这一点:
import pytest
@pytest.fixture
def myfixture():
pytest.skip('Because I want so')
def test_me(myfixture):
pass
在内部,
pytest.skip()
函数引发一个异常Skipped
,该异常继承自OutcomeException
。这些异常经过特殊处理以模拟测试结果,但不会使测试失败(类似于pytest.fail()
)。能否将定义填充到try/except
块中?@PaulH-测试将失败。那我怎么跳过这些测试呢?我想你必须单独标记这些测试,或者在测试类中标记一些东西,然后一下子跳过这些测试。可能的重复:@PaulH-不,这不是重复。给出的建议不起作用,例如我给出的建议。试试看!谢谢你教育我,谢尔盖。
$ pytest -v -s -ra r.py
r.py::test_me SKIPPED
=========== short test summary info ===========
SKIP [1] .../r.py:6: Because I want so
=========== 1 skipped in 0.01 seconds ===========