-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
How to correctly run pytest.main() programmatically multiple times? #3143
Comments
python modules cache in side the same process and due to this property of python its not advisable to run pytest.main twice from the same process while there may be exceptions to that rule, in general its more safe to run a new process since python is not safe for code reloading |
I agree, we should just add a note to the docs regarding that. |
So the recommendation is to completely close python and start a new process every time you want to test your code? I don't remember this being a problem with pytest in the past, has it changed? |
@endolith this does not have to with pytest, but how Python itself works: it caches the modules it imports. For example: # foo.py
def f(): return 1
Now if we change # foo.py
def f(): return 2 and continue in the same session:
Sorry, I'm reasonably sure pytest never supported this, because again it doesn't have to do with pytest but how Python works... and I don't think pytest had any special code in place to workaround this. |
BTW there's a note at the bottom of the docs for exactly this issue: https://docs.pytest.org/en/latest/usage.html |
@nicoddemus Apparently the reason it works for me is a side effect of running inside of Spyder: https://stackoverflow.com/questions/54009371/pytestwarning-module-already-imported-so-cannot-be-rewritten-pytest-remotedata/54011939?noredirect=1#comment94930323_54011939 and the warnings about |
Oh that explains it then. 👍 |
So what's the recommended way to do this? |
Something like this works, but is kind of ugly:
|
Indeed running as a subprocess is the recommended way to do this. |
@nicoddemus Maybe a convenience function can be written to do this? Or at least add some hints to the note at the bottom of https://docs.pytest.org/en/latest/usage.html |
I'm OK with a note to the docs. Btw, do you know the |
@nicoddemus No I don't know about |
@endolith |
For that use case @blueyed's suggestions are on point then. 👍 |
There's no way to add an option like if __name__ == '__main__':
pytest.main([__file__], newprocess=True) or something? This is the junk I have at the end of every It feels wrong, but I don't see what the right way is. |
If I invoke
pytest.main(['test_foo.py'])
multiple time from the same running script, it will give the same result, even iftest_foo.py
changes between the runs. (This was also observed in #793.)But then what is the correct way to rerun pytest programmatically? Could you point me to an example? Starting it in a subprocess feels quite clunky and also makes it more cumbersome to process the results.
(Also, could we possibly have a note about that behavior in the docs? At least to me, it was somewhat unexpected that I can't run
pytest.main()
in the same script twice.)The text was updated successfully, but these errors were encountered: