Search code examples
pythontestingpytestfixtures

How to solve issue with fixture not found in pytest?


I am trying to use the pytester fixture, to create some files in the test directory, but I get the following error:

  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.

The code I am trying to run, to create some python files in below, taken from the pytester documentation:

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.

How can I use the pytester fixture to create some files in the test directory? The link for the pytester documentation: https://docs.pytest.org/en/6.2.x/reference.html#pytest.Pytester.path


Solution

  • Try adding this line in conftest.py (or in your test file) as documented.

    pytest_plugins = "pytester"
    

    Or if you already have any existing one, just make it a list:

    pytest_plugins = [
        ...
        "pytester",
    ]
    

    Also, make sure that your version of pytest is >= 6.2 as it is the requirement based on the docs.