-
-
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
Don't auto-discover tests in particular folders #12749
Comments
id like to fix this issue. After reading the discussion I would propose something to the effect of: Add In your specific case it would be: |
@FreerGit just to make sure we are on the same page, note that in the OP the test files are in |
@nicoddemus In this case, the entire directory would not be picked up during collection. Thus never considered even if the imported. To clarify:
And then a test file in testing/ignore called
This would not fail (the tests would be ignored) with the I believe we are on the same page :) Edit: |
I might be wrong, but I don't think so. In the OP scenario, the test files are not being ignored, they should be collected normally. The problem is the imported classes (from modules in
In your example you excluded |
Perhaps an example helps:
# content of foo.py
class Testament: ... # content of test_foo.py
from foo import Testament
class TestTestament:
def test_foo(): ... In this situation, the user executes:
And pytest collects The OP would like some way to signal that classes defined in |
Yes, the entire My attempted solution was formed by:
from OP. The way I see it either a directory has to be explicitly ignored (what I proposed) or implicitly ignored by telling pytest to only considering tests from Ill try and figure out the implicit route |
Just excluding some directories from collection today is already possible via norecursedirs. I was channeling the OP here and assuming they meant that classes from these directories not to be collected ( |
Yes, @nicoddemus, that's what I'm looking for. In my example, And just so people understand that this comes from an actual need, this is for https://github.com/mlcommons/modelbench and https://github.com/mlcommons/modelgauge, where we're measuring the safety of LLMs. A Test is a core concept in the domain, so we legitimately have classes like |
Thanks for the help! Hope it wasn't too much back and forth. Let me know there's any misunderstandings or problems. |
Having a opt in that turns imported Test classes into collect time skips might be a nice way to enable this |
This inspired by a discussion: #12748
What's the problem this feature will solve?
We have a project that's about a different kind of test. We have a lot of classes with "Test" in the name, including some that start with "Test". We would also like to organize some of our pytest tests in classes whose names start with "Test". We distinguish them by directory; all our pytest tests are in
tests
, and all our production code is insrc
. We'd like an easy way to set this up with no distortion to our production code and minimal shenanigans in the test code, while getting no warnings.Naively one might think that limiting discovery to a particular directory, as with
pytest tests
ortestpaths=["tests"]
. However, if a production file called TestFoo is imported from production code into test code, it will still be discovered, resulting in a warning like:Describe the solution you'd like
For me, the best solution is just to by default disable discovery beyond the requested
testpaths
. Trying to run something just because it's included from wherever strikes me as counterintuitive behavior. If somebody needs that behavior, perhaps an option likediscover_imports
would be useful.The second-best solution would be an option where we can specify paths to definitely not discover things in. E.g.:
Alternative Solutions
There are many ways to solve this, including manually adding a
__test__ = false
to each production class, using a marker mixin like NotAPytestTest to achieve the same end, importing production classes into the tests under different names, doing only local imports, or renaming our test classes. All of these are a little clunky, and require people to remember a ritual under certain circumstances.The solution I went with is to put
after my domain imports and before my test classes. It still seems clunky to me, but only has to happen once per file of tests. I think it would be better still if this could be fixed in one spot, or have it work by default.
Additional context
Please see this GitHub discussion and this example repository that demonstrates the problem.
The text was updated successfully, but these errors were encountered: