Repository navigation
A good way to clean all the test environment? #10353
Replies: 1 comment
|
pytest deliberately runs tests in the same Python interpreter by default, so there is no reliable "reset the entire Python environment between test files" option. In particular, trying to remove everything from The usual fix is to isolate the state that each test changes:
For example: def test_something(monkeypatch):
monkeypatch.setenv("MY_SETTING", "test")pytest restores the environment change automatically after the test. If you genuinely need a completely fresh interpreter, process isolation is the correct boundary: run the relevant test file/group in a separate pytest subprocess. I would also treat the ordering dependency as something worth fixing rather than relying on a global cleanup mechanism; tests should ideally pass regardless of which test ran before them. Please mark this answer as accepted if it helped, thank you. |
Uh oh!
There was an error while loading. Please reload this page.
Hi! During a pull request on a company feature I found that there is a hidden dependency between my test cases. I wonder if there is a clean and happy way to clean the python environment between test file executions.
I would like to unload all modules / restore env variables / etc.
Any good ideas?
All reactions