EDIT: While any testing recommendations are truly appreciated, I'm wondering specifically if there'a way that pytest can enforce the isolation for me, rather than relying on myself always remembering to clean up mocks.
Does pytest support "resetting" process state between individual Python files, or otherwise isolating test files from one another?
Our CI used to invoke pytest on individual files, like this:
pytest file1.py
pytest file2.py
...
It now invokes pytest once for all files, like this:
pytest file1.py file2.py ...
We ran into trouble when some test files (e.g. file1.py) performed module-level mocks. For example (simplified):
def setup_module():
patch("path.to.some.module.var", Mock()).start()
(There is no corresponding teardown_module.)
This works fine when pytest is called on files one-at-a-time. But when it runs against multiple files, any mocks made by previously executed test code persist into subsequent test code. Is there any way to "reset" this state between files? E.g., does pytest support a way to invoke each file's tests in a separate process? (Looked at the pytest docs but found nothing like this.)
For now, we're adding teardown_module() functions that call patch.stopall(), but it would be nice to have the added security of pytest implicitly isolating our test files from one another.