-
-
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
Exit pytest if a collection error occours #1421
Comments
You can easily do this with a local plugin, e.g. by putting this in a import pytest
def pytest_collectreport(report):
if report.failed:
raise pytest.UsageError("Errors during collection, aborting") Then even if tests are collected, your hook will exit before they are run:
|
Great, it works as I was expecting! |
@fontealpina would you consider writing a short blog post with this trick in pytest-tricks? 😉 cc @hackebrot |
@nicoddemus it is a pleasure for me. I'll do it as soon as possible. |
Nice, thanks! 😎 |
Actually i think it's worthwhile to consider if we want to consider changing the default to stop if a collection error occurs. Having good defaults is a major goal IMHO. Who ever likes to execute tests when there are collection errors? Whenever i see them i usually want to first see what's going wrong with collection before considering anything else. If there is a real need we can introduce a |
Hmm, I actually agree with that - let's reopen this |
I agree... if I see collection errors the first thing I do is to immediately hit CTRL+C to stop the run. 😁 |
since the collection behavior is a "breaking" change, we should do it on a major release |
IMHO this could go into a 2.10.0 release... it is a small behavior change, and to restore the previous behavior (which I doubt there are people depending on) is just a matter of adding a line to [pytest]
addopts = --continue-on-collection-errors What IMHO should definitely go into a major release are API changes which break entire test suites. |
we are certain to break behind a firewall ci setup in a minor release then |
Sorry, what do you mean by "firewall ci setup"? |
as in we are certain to "break" testsuites in private setups that previously ran in spite of a few minor collection errors |
But those test suites were failing to anyway, no? 😉 After all a collection error does fail a test suite. |
i think the compat aim of pytest minor releases (like 2.10 compared to 2.9) is that all previously passing test suites will still pass. Wouldn't make too big promises on details such as the one we are discussing. |
why does pytest exit with code 0 when there is a collection error? I'm using
|
oh weird, this seems to only occur in PyCharm....
|
@ghostsquad which version are you using? I just did a quick test here and things seem to work as expected: # contents of foo.py
import kjanskjan
def test_foo():
pass
# contents of bar.py
def test():
pass
(Tested in pytest 2.9.0 and 2.8.7) |
@nicoddemus ya, it seems fine on the terminal, but within PyCharm CE 5.0.4, it is all "green" and exit code 0. py.tests configuration in PyCharm is default with options: I might have to bring this up to Jetbrains. |
If a collection error occurs, is there a way to terminate immediately pytest execution after collection, avoiding the execution of all the tests (even the ones properly collected)?
Basically I need to start test execution only if there are no import errors.
So for instance, now if there is a collection error I get:
I would get something like this instead:
Is there an options that allows that?
Or maybe this can be done with a plugin?
Thanks
The text was updated successfully, but these errors were encountered: