问题是我给定的 fixture 函数具有外部依赖性,这会导致“错误”(例如无法访问的网络/资源不足等)。
我想跳过 fixture ,然后跳过任何依赖于该 fixture 的测试。
做这样的事情是行不通的:
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 -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 ===========
内部,
pytest.skip()
函数引发异常 Skipped
,继承自 OutcomeException
.这些异常经过专门处理以模拟测试结果,但不会使测试失败(类似于 pytest.fail()
)。
关于pytest - 有没有办法跳过 pytest fixture ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46852934/