Skip to content

Fixture not found because of problem with 'rootdir' and 'pytest.ini' files #6359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
jnlivingston opened this issue Dec 19, 2019 · 2 comments
Closed
Labels
topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity

Comments

@jnlivingston
Copy link

I have Python 3.8.0, pytest 5.3.2 and the next directory layout:

project1
└── test_cases
    └── sample
    |   └── pytest.ini
    |   └── test_sample.py
    └── conftest.py
    └── pytest.ini

The contents of the file conftest.py:

import pytest

@pytest.fixture
def fixture_sample():
    print('FIXTURE SAMPLE')

The contents of the file test_sample.py:

def test_sample(fixture_sample):
    pass

I am in the project1 directory. Running the command py.test -vs --rootdir ./test_cases/sample:

user@machine:~/projects/project1$ py.test -vs --rootdir ./test_cases/sample
rootdir: /home/user/projects/project1/test_cases/sample
collected 1 item
test_cases/sample/test_sample.py::test_sample FIXTURE SAMPLE
PASSED

All right. But I don't see inifile: pytest.ini in the output message.
Next I run the command py.test -vs ./test_cases/sample:

user@machine:~/projects/project1$ py.test -vs ./test_cases/sample
rootdir: /home/user/projects/project1/test_cases/sample, inifile: pytest.ini
collected 1 item
test_cases/sample/test_sample.py::test_sample ERROR
file /home/user/projects/project1/test_cases/sample/test_sample.py, line 1
  def test_sample(fixture_sample):
E       fixture 'fixture_sample' not found

There is an error. I see inifile: pytest.ini in output message and rootdir in both cases is the same. Why doesn't it work? Why was the fixture 'fixture_sample' not found?

@Zac-HD Zac-HD added topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity labels Dec 20, 2019
@nicoddemus
Copy link
Member

Hi @jnlivingston,

First of all, sorry about the delay and thanks for the detailed explanation, we appreciate it!

I am in the project1 directory. Running the command py.test -vs --rootdir ./test_cases/sample
All right. But I don't see inifile: pytest.ini in the output message.

This is correct: the rootdir is not used to determine the pytest.ini, is the other way around: rootdir is determined after finding a suitable pytest.ini file.

https://docs.pytest.org/en/latest/customize.html#initialization-determining-rootdir-and-inifile

You don't see a pytest.ini in the output message because pytest did not find any pytest.ini from the current directory or upwards. The rootdir is used only to construct nodeids during collection and provide a stable location for plugins to store cross-test run state, not to use as a basis to find pytest.ini files.

Unfortunately I agree that the current interaction between rootdir and ini files is far from clear, and someone just looking at --rootdir would assume that it should point to the root of your repository/project, and from there on pytest would find pytest.ini files and start collection, but this is currently not the case.

Next I run the command py.test -vs ./test_cases/sample

Here what happens is that pytest will now find test_cases/sample/pytest.ini (according to the rules in the documentation) and it will also be considered the confcutpath, meaning that it won't go up directories where pytest.ini is located to find conftest.py files, hence it won't load up test_cases/conftest.py.

@Zac-HD
Copy link
Member

Zac-HD commented Jan 17, 2020

Closing this issue as I think the question has been answered.

@Zac-HD Zac-HD closed this as completed Jan 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: collection related to the collection phase topic: fixtures anything involving fixtures directly or indirectly type: question general question, might be closed after 2 weeks of inactivity
Projects
None yet
Development

No branches or pull requests

3 participants