pytest - 有没有办法跳过 pytest fixture ?

标签 pytest

问题是我给定的 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/

相关文章:

python - 如果没有断言,是否可以使 pytest 失败?

python - 自定义 sys.excepthook 不适用于 pytest

python - 为什么 caplog.text 是空的,即使我正在测试的函数正在记录?

python - 根据执行时间通过测试

python - 带有西里尔符号的 pytest 3.0.5

Python 覆盖率报告仅覆盖测试文件

python - 报告 pyTest 中的断言数

python - 在 pytest 中运行 Jupyter notebook 测试。操作系统错误

python - 使用 PostgreSQL 在 SQLAlchemy 测试中创建数据库

python - 为什么在 pytest-django 中使用 ThreadPoolExecutor 时会得到空的 django 查询集?