如何测试pytest夹具本身?

and*_*abs 6 python unit-testing fixtures pytest

什么是测试pytest固定装置本身的正确方法。请不要将其与在测试中使用夹具混淆。我只想自己测试灯具的正确性。

当尝试在测试中调用并执行它们时,我面临:

Fixture "app" called directly. Fixtures are not meant to be called directly

任何对此的投入将不胜感激。关于此主题的文档没有给我有意义的指导:https : //docs.pytest.org/en/latest/deprecations.html#calling-fixtures-direct

测试治具本身的动机就在于我,因为当我们的测试由于治具中的错误而失败时,这在我们的TAP文件中无法正确跟踪,这促使我独立测试治具。

hoe*_*ing 6

pytest有一个pytester插件,用于测试pytest自身和插件;它在不影响当前测试运行的独立运行中执行测试。例子:

# conftest.py

import pytest

pytest_plugins = ['pytester']

@pytest.fixture
def spam(request):
    yield request.param
Run Code Online (Sandbox Code Playgroud)

该装置spam存在一个问题,即它仅适用于参数化测试;一旦在非参数化测试中被请求,它将引发AttributeError. 这意味着我们不能通过像这样的常规测试来测试它:

def test_spam_no_params(spam):
    # too late to verify anything - spam already raised in test setup!
    # In fact, the body of this test won't be executed at all.
    pass
Run Code Online (Sandbox Code Playgroud)

相反,我们使用插件testdir提供的夹具在独立的测试运行中执行测试pytester

import pathlib
import pytest


# an example on how to load the code from the actual test suite
@pytest.fixture
def read_conftest(request):
    return pathlib.Path(request.config.rootdir, 'conftest.py').read_text()


def test_spam_fixture(testdir, read_conftest):
    # you can create a test suite by providing file contents in different ways, e.g.
    testdir.makeconftest(read_conftest)
    testdir.makepyfile(
        """
        import pytest

        @pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)
        def test_spam_parametrized(spam):
            assert spam in ['eggs', 'bacon']

        def test_spam_no_params(spam):
            assert True
""")
    result = testdir.runpytest()
    # we should have two passed tests and one failed (unarametrized one)
    result.assert_outcomes(passed=3, error=1)
    # if we have to, we can analyze the output made by pytest
    assert "AttributeError: 'SubRequest' object has no attribute 'param'" in ' '.join(result.outlines)
Run Code Online (Sandbox Code Playgroud)

为测试加载测试代码的另一个方便的可能性是testdir.copy_example方法。在 中设置根路径pytest.ini,例如:

[pytest]
pytester_example_dir = samples_for_fixture_tests
norecursedirs = samples_for_fixture_tests
Run Code Online (Sandbox Code Playgroud)

现在创建samples_for_fixture_tests/test_spam_fixture/test_x.py包含内容的文件:

import pytest

@pytest.mark.parametrize('spam', ('eggs', 'bacon'), indirect=True)
def test_spam_parametrized(spam):
    assert spam in ['eggs', 'bacon']

def test_spam_no_params(spam):
    assert True
Run Code Online (Sandbox Code Playgroud)

(它与testdir.makepyfile之前作为字符串传递的代码相同)。上面的测试变为:

def test_spam_fixture(testdir, read_conftest):
    testdir.makeconftest(read_conftest)
    # pytest will now copy everything from samples_for_fixture_tests/test_spam_fixture
    testdir.copy_example()
    testdir.runpytest().assert_outcomes(passed=3, error=1)
Run Code Online (Sandbox Code Playgroud)

这样,您不必在测试中将 Python 代码维护为字符串,还可以通过使用pytester. 您还可以通过pytester_example_path标记配置测试数据根:

@pytest.mark.pytester_example_path('fizz')
def test_fizz(testdir):
    testdir.copy_example('buzz.txt')
Run Code Online (Sandbox Code Playgroud)

将查找fizz/buzz.txt相对于项目根目录的文件。

更多的例子,肯定检查了部分测试插件pytest文档; 此外,您可能会找到如何测试 pytest 夹具是否引发异常的问题的其他答案很有帮助,因为它包含该主题的另一个工作示例。我还发现直接研究Testdir代码非常有帮助,遗憾的pytest是没有提供大量的文档,但代码几乎是自我记录的。