- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
测试 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)
pytester
运行现有的测试模块来重用它们。 .您还可以通过
pytester_example_path
配置测试数据根。标记:
@pytest.mark.pytester_example_path('fizz')
def test_fizz(testdir):
testdir.copy_example('buzz.txt')
fizz/buzz.txt
相对于项目根目录。
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/
我正面临 pytest 固定装置的一个小问题,感谢您的帮助。 我有一些功能装置,如下所述。为简单起见,我没有展示实现。 @pytest.fixture() def get_driver():
我有一个现有的 pytest使用一些预定义列表来测试它们的交叉产品的测试: A_ITEMS = [1, 2, 3] B_ITEMS = [4, 5, 6] C_ITEMS = [7, 8, 9] 我还
我要传一个pytest.fixture函数到另一个 fixture 函数的 param 参数。像这样的东西: @pytest.fixture() def foo(): return "foo"
我试图避免在我的测试中重复太多的样板文件,我想以更有条理的方式重写它们。假设我有两个不同的解析器,它们都可以将文本解析为 doc。然后该文档将用于其他测试。最终目标是公开一个可以在其他测试中使用的 d
我有很多通过 pytest 管理的参数化装置。 有时,我希望使用 fixture 的测试不必担心应用参数。 是否可以制作一个参数化另一个 fixture 的 fixture ? import pyte
我有一对 fixture 用于测试具有 x-y 输入的函数。一个生成 x 值,另一个生成 y 值。测试取决于两者。 y 值也取决于 x 值。以下是设置摘要: import pytest @pytest
例子: from pytest import fixture, skip @fixture(params=['a', 'b']) def f1(request): yield request.
我的 Karma 配置文件中的文件属性是 files: [ // Program files 'public/js/init.js',
Conftest.py @pytest.fixture(scope="module") def fixture2(request): do something @pytest.fixture(
我正在使用 unittest.mock 模拟一个 API。我的界面是一个在后台使用 requests 的类。所以我正在做这样的事情: @pytest.fixture def mocked_api_an
基于此堆栈溢出:pytest fixture of fixtures 我在同一个文件中有以下代码: @pytest.fixture def form_data(): return { ...
命令行 python3 -m pytest src/spec/ --app=android conftest.py import pytest def pytest_addoption(parser)
我正在对我的代码进行一些测试,我得到了我的第一个“停止”,因为我不知道如何继续前进。在我加载灯具的 setUp() 函数中看到: public function setUp() { stati
我在两种模式下运行测试:裸 pytest 和 pytest-xdist。我有一个用模块范围定义的重型 fixture 。在这个 fixture 中,我对使用 xdist 运行测试的情况进行了一些优化:
我想在相同参数化测试的不同实例化之间共享 fixture,其中 fixture 本身也被参数化: #!/usr/bin/py.test -sv import pytest numbers_for_fi
我有一个包含多个 JSON 文件的 AWS S3 目录,这些文件用作测试输入。 我创建了一个 PyTest 模块,它使用模块范围的 fixture 下载所有 JSON 文件一次,然后运行多个测试函数
我有一个固定装置,它返回该端点名称(传入)的端点 名称是测试中设置的字符串。我在测试中每次调用端点(参数化)都搞砸了,现在我不知道如何在不每次调用端点的情况下获得相同的功能。 基本上,我只需要调用一次
如果我有两个参数化的 fixture ,我如何创建一个测试函数,首先用一个 fixture 的实例调用,然后用另一个 fixture 的实例调用? 我想创建一个以某种方式连接两个现有装置的新装置是有意
我是python的新手。我已经声明了一个装置 configure_loggers()范围为“ session ”。现在,我想在另一个 fixture 之前使用这个 fixture setup_app(
我想在另一个固定文件中实例化的固定文件中使用一个对象。类似于以下内容(不起作用): monitor_france: objecttype_id: 2 name1: i-france-1 n
我是一名优秀的程序员,十分优秀!