gpt4 book ai didi

python - 如何测试 pytest fixture 本身?

转载 作者:行者123 更新时间:2023-12-04 12:38:51 26 4
gpt4 key购买 nike

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

当尝试在测试中调用并执行它们时,我面临:
Fixture "app" called directly. Fixtures are not meant to be called directly
对此的任何意见将不胜感激。关于这个主题的文档没有给我有意义的指导:https://docs.pytest.org/en/latest/deprecations.html#calling-fixtures-directly

测试 fixture 本身的动机是我,因为当我们的测试由于 fixture 中的错误而失败时,这在我们的 TAP 文件中没有正确跟踪,是什么促使我单独测试 fixture 。

最佳答案

pytest有一个 pytester 为测试而制作的插件pytest本身和插件;它在不影响当前测试运行的隔离运行中执行测试。例子:

# conftest.py

import pytest

pytest_plugins = ['pytester']

@pytest.fixture
def spam(request):
yield request.param

治具 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

相反,我们使用 testdir 在隔离的测试运行中执行测试。 pytester 提供的 fixture 插入:
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)

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

现在创建文件 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

(它与之前作为字符串传递给 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)

这样,您不必在测试中将 Python 代码作为字符串维护,还可以通过使用 pytester 运行现有的测试模块来重用它们。 .您还可以通过 pytester_example_path 配置测试数据根。标记:
@pytest.mark.pytester_example_path('fizz')
def test_fizz(testdir):
testdir.copy_example('buzz.txt')

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

有关更多示例,请务必查看 Testing plugins 部分在 pytest文档;此外,您可能会发现 my other answer提问 How can I test if a pytest fixture raises an exception?很有帮助,因为它包含该主题的另一个工作示例。我还发现学习 Testdir code 非常有帮助。直接悲哀地 pytest没有为它提供广泛的文档,但代码几乎是自记录的。

关于python - 如何测试 pytest fixture 本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56631622/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com