Trouble running individual pytest tests in a django project in a docker container
Greetings.
SETUP:
PyCharm 2022.1.3 (Build #PY-221.5921.27, built on June 21, 2022)
Docker Desktop 4.4.2 (73305)
Python 3.9.4
Django 4.2
Pytest 6.2.4
I've been struggling with this one for a couple of days now, and I'm hoping someone can help point me in the right direction.
I have a Django project that has a dockerfile, and uses docker-compose to bring up required external services like DBs and caches and such.
I have been able to set up a run configuration for this project that will successfully run all the tests in any given area of the app using the run configurations menu.
Here's what one of those configurations looks like:
As you can see, it's pretty simple. Just point to the path I'd like to run tests under, and use the one docker-compose interpreter I've set up for this project. Woot!
The problem comes when I try to use the feature to allow running an individual test by clicking on the green arrow next to that test in the editor window:
Whenever I use this UI to start a test, the test fails to run. In the "run" window pane I see the command that starts the test run:
/usr/local/bin/docker-compose -f /project/path/docker-compose.yml -f /project/path/docker-compose.override.yml -f /home/path/Library/Caches/JetBrains/PyCharm2022.1/tmp/docker-compose.override.264.yml up --exit-code-from web --abort-on-container-exit web
Testing started at 1:05 PM ...
Then, I see docker compose starting up my set of containers. And once the test container is up, I see logs from within that container:
web_1 | Environment: docker
web_1 | Role: docker
web_1 | Use datadog: false
..... (more logs from our container startup process here)
web_1 | Launching pytest with arguments test_general.py::test_blank_identity_cannot_be_created --no-header --no-summary -q in /app
web_1 |
web_1 |
web_1 | Test session starts (platform: linux, Python 3.9.4, pytest 6.2.4, pytest-sugar 0.9.4)
web_1 | cachedir: .pytest_cache
web_1 | django: settings: custom_settings.docker (from env)
web_1 | hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/app/.hypothesis/examples')
web_1 | rootdir: /app, configfile: pyproject.toml
web_1 | plugins: django-4.2.0, mock-3.6.1, cov-2.12.1, hypothesis-6.14.6, flake8-1.0.7, sugar-0.9.4, pycodestyle-2.2.0, Faker-8.11.0, ddtrace-0.51.1
web_1 | collecting ... web_1 |
web_1 |
web_1 | Results (0.20s):
web_1 | ERROR: file or directory not found: test_general.py::test_blank_identity_cannot_be_created
It appears to me that the test runner expects to be executing in the context of the directory where the module for my specific test is located. But when pytest starts, it reports the rootdir as the root of my project inside the container: `/app`
I've looked at the docker-compose-override file that PyCharm generates for this job and here's what it contains:
version: "3"
services:
web:
command:
- "python"
- "-u"
- "/opt/.pycharm_helpers/pycharm/_jb_pytest_runner.py"
- "--target"
- "test_general.py::test_blank_identity_cannot_be_created"
environment:
PYTHONPATH: "/opt/project:/opt/.pycharm_helpers/pycharm_matplotlib_backend:/opt/.pycharm_helpers/pycharm_display:/opt/.pycharm_helpers/pycharm"
PYTHONUNBUFFERED: "1"
PYTHONIOENCODING: "UTF-8"
PYTEST_RUN_CONFIG: "True"
PYCHARM_HOSTED: "1"
PYCHARM_DISPLAY_PORT: "63342"
PYCHARM_HELPERS_DIR: "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm"
PYTHONDONTWRITEBYTECODE: "1"
stdin_open: true
volumes:
- "/Users/cpe/projects/Project-Vitamin:/opt/project:rw"
- "pycharm_helpers_PY-221.5921.27:/opt/.pycharm_helpers"
working_dir: "/opt/project/apps/account/tests"
volumes:
pycharm_helpers_PY-221.5921.27: {}
At this point I'm a little bit at a loss. I'd love to be able to use the editor UI for running individual tests, it's more ergonomic for my team to do so when they are running individual tests, but I can't seem to untangle this path problem.
Is there some way to get the UI in the editor to use a different form of docker-compose override for its work? The run configuration I set up also uses an override, and it works perfectly. It uses the `--path` form instead of the `--target` form:
version: "3"
services:
web:
command:
- "python"
- "-u"
- "/opt/.pycharm_helpers/pycharm/_jb_pytest_runner.py"
- "--path"
- "apps/account"
environment:
PYTHONPATH: "/opt/project:/opt/.pycharm_helpers/pycharm_matplotlib_backend:/opt/.pycharm_helpers/pycharm_display:/opt/.pycharm_helpers/pycharm"
PYTHONUNBUFFERED: "1"
PYTHONIOENCODING: "UTF-8"
PYTEST_RUN_CONFIG: "True"
PYCHARM_HOSTED: "1"
PYCHARM_DISPLAY_PORT: "63342"
PYCHARM_HELPERS_DIR: "/Applications/PyCharm.app/Contents/plugins/python/helpers/pycharm"
PYTHONDONTWRITEBYTECODE: "1"
stdin_open: true
volumes:
- "/Users/cpe/projects/Project-Vitamin:/opt/project:rw"
- "pycharm_helpers_PY-221.5921.27:/opt/.pycharm_helpers"
working_dir: "/opt/project"
volumes:
pycharm_helpers_PY-221.5921.27: {}
Please sign in to leave a comment.