-
Notifications
You must be signed in to change notification settings - Fork 345
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
Provide a way to detect if Django is running tests #333
Comments
|
You can add a setting yourself with a separate settings file for tests |
I think the approaches suggested by @blueyed and @adamchainz should be sufficient if this is needed. Generally, changing the way your code works by introspecting whether or not the code is called from tests is not recommended. If this were to be done, it could be done in a general pytest way (it is not really specific to pytest-django). Thanks for the suggestion, feel free to reopen this issue if you have additional information in this issue! |
Relevant example from the pytest docs Detect if running from within a pytest run:
The problem is that |
@axil |
Hmm, yes, got it working, though it looks a bit hacky for me. Another quick and dirty way to check this in settings.py is
|
The fact the pytest is imported does not mean that |
Here is very valid use case: I have something like:
Using |
I had to change it to |
I find all of the solutions above a bit fragile. They are easily broken when running tests from PyCharm, for example, where executable in sys.args is: While we can account for this edge case, we certainly never can be sure that this hack will work with other edge cases.
Any solution that involve Setting env variable like @pelme, I agree with the point that it's mostly not recommended to change the code behaviour based on whether tests are running or not, but as you can see, there's still a demand for it. Therefore, I thing this library should provide built-in consistent indicator that tests are running. |
This might be over engineered but I think this works better and helps to avoid one of the edge case with
Do let me know if theres a case where this will not work |
It'd be nice to do something like:
The text was updated successfully, but these errors were encountered: