Set Default Working Directory of Pytest in PyCharm

IDEs are cool and all, but in their magic, they can change configurations that you wanted to stay the same.

pytest Norms

Many pythonistas write pytests that assume the project root to be the working directory.

PyCharm is making the test script that is run the working directory. And when you run a new test, any adjustments you made to the working directory configuration are changed.

Making changes permanent

To keep the configuration you want permanent within the PyCharm test runner, you'll want to do the following:

Edit the default test runner for the project to be pytest (or whatever you specifically use). To do this, open Preferences > Python Integrated Tools > Default test runner. Select pytest.

edit default test runner

Edit the Configuration Template for pytest. To do this, open Edit Configurations > Edit configuration templates > Python tests > pytest. Set Working directory to the Content Root, or project root.

edit configuration for pytest

Note that any test Configurations that have already been configured will not have these defaults set. You'll likely want to delete and re-create them (or edit each one as described above).

Run tests. Adhoc pytest Configurations will be spun up. They should inherit these defaults.