Skip to content

Syntax error in test file makes pytest exit with exit code 2 #4603

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

Open
martinsmid opened this issue Jan 5, 2019 · 3 comments
Open

Syntax error in test file makes pytest exit with exit code 2 #4603

martinsmid opened this issue Jan 5, 2019 · 3 comments
Labels
topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch

Comments

@martinsmid
Copy link

Ubuntu 18.04
pytest-4.0.2
python 2.7 and 3.6

Description

Having a test file with syntax error or import error results in pytest exiting with exitcode 2.

How to reproduce

  1. Create a file named test_syntax_error.py with content
def x:
    pass
  1. Run pytest and print exit code
    pytest test_syntax_error.py; echo $?

Expected

Probably worth a new exitcode as it doesn't really fit into those 5 documented

Actual

Exits with code 2, which stands for "Test execution was interrupted by the user"

Similarly, a file with content import non_existent will have the same effect.

This issue is similar to #2832 and also
#2950, but still I hope this will be useful for reference and example.

@nicoddemus nicoddemus added type: enhancement new feature or API change, should be merged into features branch topic: config related to config handling, argument parsing and config file labels Jan 10, 2019
@nicoddemus
Copy link
Member

Hi @martinsmid,

In this case we raise Interrupted because we decided some time ago that we wanted to interrupt the running session in case of a collection error. I guess it would be fine though to include a new EXIT_COLLECTIONERROR exit code instead of reusing the Interrupted exception.

@symonk

This comment has been minimized.

@gnikonorov gnikonorov changed the title exit code 2 Syntax error in test file makes pytest exit with exit code 2 Jun 11, 2020
@BenjaminHamon
Copy link

Sorry about resurrecting a very old issue but I just ran into this and didn't find a more recent issue. I have a tool invoking pytest and checking error codes to raise the adequate exception and message. Getting the exit code 2 (Interrupted by user) was very weird until I figured out it was an import error during collection.

It seems to me it was unwise to reuse the Interrupted exit code for a totally different exit case. This behavior is also undocumented at https://docs.pytest.org/en/stable/reference/exit-codes.html.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic: config related to config handling, argument parsing and config file type: enhancement new feature or API change, should be merged into features branch
Projects
None yet
Development

No branches or pull requests

4 participants