-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Attempt to clarify the confusion regarding __init__ files and unique test names #2297
Conversation
@@ -154,11 +150,6 @@ options. It will run tests against the installed package and not | |||
against your source code checkout, helping to detect packaging | |||
glitches. | |||
|
|||
Continuous integration services such as Jenkins_ can make use of the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Took this out because it seemed like out of place in the tox
section.
I'd add another "test layout". The https://blog.ionelmc.ro/2014/05/25/python-packaging/ |
While I completely agree with the points in @ionelmc's post, I'm not sure it is a good fit for this particular section of the docs. The section is titled Choosing a test layout and it is all about choosing either embedding the tests as part of the package or putting them in a separate folder which is not part of your package; having your root package at the root of the repository or at a But again, I'm not sure, so I would like to hear more opinions (@ionelmc please do chime in if you want). |
The src-layout is more concerned with where the code is, rather than where the tests are. It's true that my blog post touches the subject of test location but that's rather a problem of me tackling too many problems in one article :) |
The "Tests outside application code" section says:
This statement is not (entirely) true. You can easily have non-unique names if you add In the "Tox" section I'd mention the |
You are right, it should be mentioned that adding
Why is that, could you clarify this bit?
I'm not aware of this solution as well, could you explain this in more details? Thanks! |
Suppose that you need to use duplicate test names and you create the following layout:
When you run A better approach is:
Now it doesn't matter anymore if you have an
then the directory in |
Many thanks for the detailed explanation, I have clear picture now of all the relevant issues here. I will work on adding more info to the docs tomorrow. Thanks again! |
__init__.py | ||
test_view.py | ||
|
||
Now pytest will load the modules as ``tests.foo.test_view`` and ``tests.bar.test_view``, allowing |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't work. pytest
uses the full directory path as import name. You can choose between using __init__.py
files or not, but if you do then you have to put them everywhere or it'll break.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this section is complex (not complicated), because this is a complex problem with different solutions. I suggest to document all of them as it's hard to figure out these things (probably impossible for a newbie).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but if you do then you have to put them everywhere or it'll break.
Are you certain? I just tested this and it works:
.tmp/
tests/
foo/
__init__.py
test_foo.py
bar/
__init__.py
test_foo.py
Executing pytest
from .tmp
works as expected, with only .tmp/tests
being added to sys.path
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try this:
.tmp/
tests/
foo/
__init__.py
test_foo.py
email/
__init__.py
test_email.py
pytest
will import email.test_email
, but ooops email
is a stdlib package...
The same goes with an installed package. It will try to import mypkg.test_xxx
but there isn't a test_xxx.py
in your installed mypkg
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Applied your suggestions, let me know what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Fix #529