-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Calling pytest.main() without arguments unpredictably reads arguments from the command line #11183
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
This is the standard pattern for callables that get used as console script entry point |
But |
@antosikv FWIW I agree it would have been better if However it should definitely be clear in the docs. Would you like to send a PR updating the docs? (You don't need to worry about building the docs locally etc, when you send a PR there is a link to the rendered docs) |
@bluetech Ah, makes more sense if |
In my opinion, it would be quite reasonable to assume that running
would be equivalent to simply executing
pytest
from the command line with no arguments. The documentation even seems to suggest that this should be the case:Instead, the default value of
args
passed into the function isNone
, in which case we end up reading arguments from the command line. That seems to be a fairly weird choice for the "default" behavior, since we are now trying to use arguments passed to a completely different program. At best, this can easily lead to complaints about unrecognized options and at worst, if we happen to share some options with the calling script, possible misinterpretation of those options and subsequent unexpected behavior.It seems to me that automatically reading arguments directly from the command line would only be reasonable and expected when
pytest
is executed directly from the command line. When called programmatically from a script, it would probably be more predictable to just assume having no arguments unless explicitly specified otherwise.This would obviously introduce a breaking change for those relying on
main()
silently reading arguments from the command line but that frankly does not seem like a good practice anyway. And considering that it would be a trivial single-line change of usingmain(sys.argv[1:])
instead, it definitely seems like a good idea to be explicit about it.Anyway, if introducing a breaking change would be unacceptable, despite the arguably more predictable default behavior, it would probably be a good idea to at least amend the documentation and add an explicit warning about this behavior.
The text was updated successfully, but these errors were encountered: