Run/debug Django unittests from the context menu in PyCharm Community Edition
This is for PyCharm Community Edition which does not have any Django integration. PyCharm Professional Edition supports Django Test out of the box.
Supported configurations
PyCharm versions
Tested on:
- 2022.2
- 2020.3.3
- 2020.1
Not supported:
- ≤ 2019.2.3
Python versions
Tested on:
- 3.9
- 3.8
- 3.7
Not supported:
- 3.*, ≤3.5
- 2.*
Instruction
1. Adjust PyCharm internals
With the script (on Windows)
Requirement: patch
. Install it with Chocolatey:
choco install cyg-get
cyg-get patch
Download or clone this repo and run install.bat (as administrator), e.g.:
.\scripts\install.bat "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2022.2"
To uninstall run uninstall.bat (as administrator), e.g:
.\scripts\uninstall.bat "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2022.2"
Manually
-
Try to run test from the context menu. You will see something like:
C:\Programs\Python38\python.exe "C:\Program Files (x86)\JetBrains\PyCharm Community Edition 2019.3.2\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --target example.tests.test_foo.FooTestCase.test_bar Testing started at 16:15 ...
Note the path to
_jb_unittest_runner.py
. -
Apply patch
_jb_unittest_runner.py.patch
toYOUR_PYCHARM_INSTALL_DIR/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py
-
Copy
_jb_django_test_runner.py
toYOUR_PYCHARM_INSTALL_DIR/plugins/python-ce/helpers/pycharm/_jb_django_test_runner.py
.
2. Edit the run/debug configuration template
-
Go to the menu Run > Edit Configurations...
-
Go to Templates > Python tests > Unittests
-
Set the Working directory to the root path of your project (where
manage.py
file is located) -
In the Environment variables add a new one with name
DJANGO_TEST_MODE_GAINARIE
and any value -
Remove all previously added run/debug configurations that inherit this template.
When modifying a Template configuration, all previously added configurations that inherit it won't be updated, so they have to be manually removed. They will be automatically readded when tests run from the context menu.