Skip to content

Running individual test files doesn't work without conftest.py #5785

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
devonestes opened this issue Aug 26, 2019 · 6 comments
Closed

Running individual test files doesn't work without conftest.py #5785

devonestes opened this issue Aug 26, 2019 · 6 comments

Comments

@devonestes
Copy link

When I try and run an individual test without a conftest.py file in the root directory of my project, it appears that it's not loading modules correctly. Here's a screenshot of what I'm doing.

pytest_bug

The imports at the top of that test are:

import pytest
import datetime
import uuid
import kids.database as database

I would expect that pytest would have the same functionality without a conftest.py file as it would with an empty conftest.py file, but that appears not to be the case.

@The-Compiler
Copy link
Member

Duplicate of #2269

@The-Compiler The-Compiler marked this as a duplicate of #2269 Aug 26, 2019
@pfctdayelise
Copy link
Contributor

pfctdayelise commented Aug 26, 2019

Hmm, I think you have run into #2269 and interpreted it as a feature. :)

I would guess that running python -m pytest tests/database/test_database.py also works with no conftest.py?

Basically, you need a way for your tests to know where to find the kids code to be able to install it. Running python -m pytest is one way to do that, because you're in the kids directory. The empty conftest.py is having the same effect.

It looks like you don't have the 'kids' package installed in your virtualenv. Perhaps it is not even a package. The good practices are to use a virtualenv, make your code a package and install it "editable" in your virtualenv. If that's not feasible, I'd stick with the empty conftest.py or using python -m pytest.

@The-Compiler
Copy link
Member

The-Compiler commented Aug 26, 2019

Note that the ModuleNotFoundError is probably the correct behavior here: The current directory isn't necessarily in sys.path (where Python looks for modules) - it only happens to work because pytest adds it to load the conftest.py file.

IMHO, the proper solution is using a virtualenv where you have a development install of your project, and/or tox.

edit: @pfctdayelise was faster!

@pfctdayelise
Copy link
Contributor

Would be another good documentation page, "ModuleNotFoundError: the definitive guide to how pytest can find and import your code"

@devonestes
Copy link
Author

If you want this to throw an error, shouldn't it also throw an error when conftest.py is found?

That's what's so confusing to me here. How does an empty file explain a change in behavior? There's no configuration I'm doing there, so I don't see why any behavior would change.

@The-Compiler
Copy link
Member

If you want this to throw an error, shouldn't it also throw an error when conftest.py is found?

It should, which is why #2269 is open 😉

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