如何解决在 pytest 中找不到夹具的问题?

How to solve issue with fixture not found in pytest?

我正在尝试使用 pytester fixture 在测试目录中创建一些文件,但出现以下错误:

  def test_something(pytester):
E       fixture 'pytester' not found
>       available fixtures: _configure_application, _monkeypatch_response_class, _push_request_context, accept_any, accept_json, accept_jsonp, accept_mimetype, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, class_mocker, client, client_class, config, cov, doctest_namespace, live_server, mock_sparql_endpoint, mocker, module_mocker, monkeypatch, no_cover, package_mocker, pytestconfig, quotation_access_db_columns, quotation_access_db_path, record_property, record_testsuite_property, record_xml_attribute, recwarn, request_ctx, session_mocker, sparql_query1, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory
>       use 'pytest --fixtures [testpath]' for help on them.

我正在尝试 运行 的代码,以在下面创建一些 python 文件,取自 pytester 文档:

def test_something(pytester):
    # Initial file is created test_something.py.
    pytester.makepyfile("foobar")
    # To create multiple files, pass kwargs accordingly.
    pytester.makepyfile(custom="foobar")
    # At this point, both 'test_something.py' & 'custom.py' exist in the test directory.

如何使用 pytester fixture 在测试目录中创建一些文件? pytester 文档的 link: https://docs.pytest.org/en/6.2.x/reference.html#pytest.Pytester.path

尝试在 conftest.py(或您的测试文件)中将此行添加为 documented

pytest_plugins = "pytester"

或者如果您已经有任何现有的,只需 make it a list:

pytest_plugins = [
    ...
    "pytester",
]

此外,请确保您的 pytest is >= 6.2 as it is the requirement based on the docs.

版本