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

pytest has no documented way for the user to point it at local source files. #1937

Closed
skylarjhdownes opened this issue Sep 15, 2016 · 8 comments

Comments

@skylarjhdownes
Copy link
Contributor

From the digging I've been doing, it seems like the way to get pytest to add your local code to sys.path is to place a conftets.py and pytests.ini file in the base directory of your project. Since I haven't been able to find a mention of this in the documentation I'm hoping a dev can shed some light on whether this is a supported feature or not, and if there's a more canonical way of doing it.

Relevant SO posts:
http://stackoverflow.com/questions/34466027/in-py-test-what-is-the-use-of-conftest-py-files
http://stackoverflow.com/questions/39499937/is-there-a-standard-way-to-tell-py-test-to-run-against-specific-code

skylarjhdownes added a commit to skylarjhdownes/pytest that referenced this issue Sep 15, 2016
@nicoddemus
Copy link
Member

Hi,

I would say the canonical way is to create a virtual environment and installing your code on it in development mode (python setup.py develop).

I'm not sure it should be pytest's responsibility to add the test code to sys.path, as it is not (for example) pytest's job to download your code's dependencies automatically.

@skylarjhdownes
Copy link
Contributor Author

That makes a lot of sense, thanks. Regardless of what pytest 's responsibilities should be, placing a conftests.py in the root of your project seems to cause it to stick your local project in sys.path. I'd really like to add a mention of this to the documentation, if only because it would have saved me time.

It seems like this feature would be useful to people who can't install the codebase under test. Automatically adding modules to sys.path is also the default behavior in nose, so being able to configure pytest that way could help people making the transition from nose to pytest.

@nicoddemus
Copy link
Member

placing a conftests.py in the root of your project seems to cause it to stick your local project in sys.path.

Perhaps what you are seeing here is the default python behavior of adding CWD to sys.path automatically?

@skylarjhdownes
Copy link
Contributor Author

I have a project installed using python setup.py install. I also have a copy of the same project in my own folder. I was running the tests in my copy of the project using nosetests, which was picking up the local changes I had made. I tried running the tests using py.test (Both in the root folder and inside /test) and it used the installed version, ignoring my local changes (Incidentally, python -m pytest did pick up my local changes, but only from the project root. Probably the default Python behavior you mentioned.) Adding both a conftest.py and pytest.ini file to the root level of my project caused py.test to pick up the local changes again.

Simone Zandara mentions this behavior as a "hidden feature" in their stackoverflow post.

@nicoddemus
Copy link
Member

Thanks for the detailed explanation, I didn't know about this feature myself. 😅

I will try to check later where that happens to confirm that this is a hidden-feature of rootdir or something else.

@skylarjhdownes
Copy link
Contributor Author

Sounds good, thanks for talking through this with me. I'll draft some documentation changes and make a pull request in a bit so we can get that discussion going.

skylarjhdownes added a commit to skylarjhdownes/pytest that referenced this issue Sep 15, 2016
skylarjhdownes added a commit to skylarjhdownes/pytest that referenced this issue Sep 15, 2016
@skylarjhdownes
Copy link
Contributor Author

Would it be more useful for me to make a pull request to the master branch, or to the reorganize-docs branch?

@nicoddemus
Copy link
Member

master please 😁

This was referenced Mar 6, 2018
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

2 participants