简体   繁体   中英

Pytest Django Discovers Fixture but Says "Not Found" on the Run

I have a project where each app has a tests module as below:

app1/
  tests/
    __init__.py
    # whatever test files are
app2/
  tests/
    __init__.py
    # whatever test files are

And also have the project package in the project root as you can guess. It has a testing subpackage, which contains base test cases for models and fields and, of course, fixtures as below:

myproject/
  __init__.py
  # settings and related files, you know
  testing/
    __init__.py
    fixtures.py  # for fixtures
    # other modules for base test classes and stuff

So, my overall project structure is:

myproject/  # contains standard django stuff and a testing subpackage
app1/  # contains app and tests
app2/  # so as above
# so on and so forth

And the pytest.ini content in project root is as below:

[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.development
python_files =
    test_*.py
    myproject/testing/fixtures.py
addopts = -s
console_output_style = count
log_cli_level = DEBUG
log_print = True

So, assuming myproject/testing/fixtures.py contains a fixture as below:

# assuming i've imported related stuff

@pytest.fixture  # might have different scope
def foo():
    """does foo"""
    return "foo"

And later on, I do pytest --fixtures to see if pytest properly discovers my fixture and so it does, which means there should not be a problem.

However, when I run tests in the project, I get the error below:

E       fixture 'foo' not found

This is odd since I can clearly see it in pytest --fixtures . I do not have any idea why this happens or what causes this.


Environment

  • Python 3.7.3
  • Pytest 4.4.0
  • pytest-django 3.4.8
  • Django 2.2

If you have fixtures that should be kept in a separate module and accessible by the whole test suite, best is to register the module as a plugin to ensure it loads before the test collection phase. Create a file named conftest.py in the project root dir with the content

pytest_plugins = ['myproject.testing.fixtures']

Of course, you can just use the conftest.py instead of the fixtures.py and put the fixtures there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM