Skip to content
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

Nested conftest.py #1931

Closed
Ignalion opened this issue Sep 14, 2016 · 3 comments
Closed

Nested conftest.py #1931

Ignalion opened this issue Sep 14, 2016 · 3 comments

Comments

@Ignalion
Copy link

Hi guys,

We have integrated pytest and actually we're using it for a couple of month. You're done the great work, so far it is the best testing framework for Python I've ever seen.

However, now we moved our tests in different folders and we'd like to have some hierarchy in our fixtures and conftest.py files.
Let's say, we have nested test directory structure and I want to have like general fixtures in parent conftest.py and component-like fixtures in the nested conftest.py.

So fat pytest seems to use only the deepest one. Is it possible to make one test use fixtures from different conftest.py files?

Thanks,
Constantine

@The-Compiler
Copy link
Member

Do you have an example project somewhere? Using fixtures from a root conftest.py as well as fixtures from a conftest.py in some subdirectory from a test in that subdirectory should work without any issues.

@Ignalion
Copy link
Author

Well, indeed it is working correctly. I think there was some misunderstanding with my colleagues of what they're trying to achieve. Sorry about that and thanks for your time!

@MaheshKok
Copy link

Just a thought ... I too face the same issue

Here is how i handled it

Session related fixtures are kept in root directory
and module relates fixture in corresponding modules

all sub conftest imports from root conftest and this way sub conftest has their and parents conftest features and this helps in maintaining a readable and clean code.

rohinb2 added a commit to run-house/runhouse that referenced this issue Nov 30, 2023
When setting up the fixture list within a class, we were doing something along the lines of:

```
class TestClass:
    UNIT = {"cluster": [local_cluster]}
```

This requires an import of `local_cluster` from conftest. Turns out, this causes session scoped fixtures to be recomputed (pytest-dev/pytest#3217). It's bad practice to import from conftest in the first place. So, I changed the fixtures that run `getfixturevalue` to just use the string argument, and we can avoid importing, and rely on pytest to set up the fixtures correctly. Now we set up fixtures like so:

```
class TestClass:
    UNIT = {"cluster": ["local_cluster"]}
```

Moreover... if a fixture is defined in a nested `conftest.py`, and imported in the global `conftest.py`, then there can be conflicts. The way tests work is that they'll look heirarchically upward in conftest.py files, and then grab the first fixture they find that matches the name. Even if the global `conftest.py` imports from a nested `conftest.py` file, it is considered a different fixture object (unintuitive, yes). This will sometimes lead to session scoped fixtures being initialized twice in the same session. More info on nested conftest.py files [here](pytest-dev/pytest#1931).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants