-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
How to make testing utilities importable when using importlib mode #8964
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
Comments
I would propose that we investigate how to consider root paths in the scheme With pep 420 and various other details we should consider test to be in packages with surrounding Code, not free-standing files |
By "root paths" do you mean our own Either way, could you please elaborate more on your underlying idea? |
the idea would be that testsuites have "root folders" that either locate inside a python package, or inside a "testpath" that way by writing down the "testpaths" in pytest.ini, we could determine which modules to consider as toplevel modules also we could supply a custom import to path based testsuites i would like to elaborate the idea of "testusites" anyway, as it would be nice to be able to reuse/parametrize "testsuites" a different package ships, but thats out of scope for here bascially i would like us to enable the importlib import to "seem" like the testpath which is a parent folder of the acted like a sys.path entry for enabling imports, while distinctively not being in sys.path |
Coming from #9109 (comment), would it be possible to import the test path in case it is a package? For example running
could try to import |
Meanwhile I think you can use https://github.com/okken/pytest-srcpaths for that. |
@nicoddemus If I understand One of the reasons we wanted to use So if we add the project root with |
Unfortunately something needs to be added to As a workaround, you might consider moving your tests to a separate folder, and add that to To exemplify, consider you have this today:
Inside You could create a new
|
I'm a little rusty on the details, but can't we use I'll write a small plugin as proof-of-concept if I find the time. What would be a good place to perform that import? |
Only if you import it and add to
I'm not entirely sure what you want to do, but that seems like a good hook to do stuff before test modules themselves are imported. |
Just to add my 2 cents to this discussion: I wish that pytest had a way to treat each test script in the same way that python treats standalone scripts. In other words, for the directory layout below, I wish that running
Here's some pseudocode outlining how I imagine this working more specifically:
I think this approach would handle a lot of use cases very intuitively, although I admit that I haven't spent much time thinking about this issue, and I'm sure there are corner cases I haven't considered. For what it's worth, I run into problems with pytest's import rules when I try to write tests for my documentation examples. Briefly, I try to include a lot of examples in my documentation, and I usually make a directory for each example. Each directory includes the example code itself, any secondary files necessary to run the example code (e.g. images, data sets, etc.), metadata relating to how to display the example, and tests to make sure the example actually works. (Doctest works for simple examples, but pytest is better for more complicated ones.) I run into problems when I inevitably want to give the test script the same name (e.g. |
I've hacked together https://github.com/pmeier/pytest-import. It needs a lot of refinement, but for now it does what I want it to do: with this plugin installed, I can use common utilities while running with |
The
--import-mode=importlib
mode does not changesys.path
in order to import test modules andconftest.py
files, which has many benefits but also some drawbacks.Often in test suites there are functions and classes used only for testing. This is not a problem when the tests are embedded in the source, but for layouts where the tests are in a separate directory, users don't have many options where to put those testing-only functions/classes in a way that is importable when using
--import-mode=importlib
(see #7245 (comment)).One solution might be to have a new option that appends one or more directories to
sys.path
when running the tests only:This at first might seem similar to just going ahead and using
--import-mode=append
, however this has the advantage that any directory can be added, instead ofsys.path
being implicitly changed just becausetests/conftest.py
exists with--import-mode=append
.This is just an idea, opening this issue to discuss the problem of how can we import testing utilities when using
imporlib
mode in layouts where tests are outside the source code.The text was updated successfully, but these errors were encountered: