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 default src layout tweak #309

Open
rnickle opened this issue Apr 25, 2022 · 1 comment
Open

Pytest default src layout tweak #309

rnickle opened this issue Apr 25, 2022 · 1 comment

Comments

@rnickle
Copy link

rnickle commented Apr 25, 2022

Claudio,

Thank you very much for this comprehensive article, which I discovered from a recent Talk Python to Me podcast (362).

I was struck by the use of src layout, which I have seen before but did not understand. I immediately tried to adopt it for a
small project, but was struck that if I created a blank project with src layout, pytest was not working by default.

For example:

$ poetry new --name rnickle_template --src rnickle_template
$ ls
README.rst  poetry.lock  pyproject.toml  src  tests
$ pytest | grep ^E
E   ModuleNotFoundError: No module named 'rnickle_template'
ERROR tests/test_rnickle_template.py

There's an easy fix:

$ touch src/conftest.py
$ pytest
[ redacted ]
= 1 passed in 0.39s =

Or in pytest >= 7:

[tool.pytest.ini_options]
pythonpath = [
  "src"
]

Perhaps we can add a comment about that to the setup page?

Thanks,

Rick

@cjolowicz
Copy link
Owner

Hi @rnickle thank you for the feedback!

You need to install your project for pytest to be able to import it. This is by design. Without the src layout, pytest will import your project from the source tree, which can lead to problems if the source tree is dirty or when there are packaging issues. Testing the installed version of your project ensures that you're testing the version of the code that your users get when they install your package.

There are different ways to do this. The easiest (but not the best way) is to use poetry install, then poetry run pytest. This assumes that you have added pytest as a development dependency. The reason it's not the best way is that Poetry installs your package using an editable install, which amounts to running tests against your source tree. (An editable install is essentially a glorified symlink.)

For this reason, I would recommend to use something like Nox, or at least virtualenv. The later articles in the series explain how to do this. (Also check out nox-poetry for better integration of Nox and Poetry. It's not mentioned in the blog, but it's a part of the companion project template.)

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