How to fix double run testing with docker in Django
Oct. 25, 2023, 9:48 a.m.
When I first switched to using docker and docker compose, Pycharm for some reason started running tests twice. Moreover, in the test debugging mode, testing was launched once, as expected.
The created containers when running the tests did not contain duplicate code on the file system, and Pycharm used the standard test run command:
/app/.venv/bin/python /opt/.pycharm_helpers/pycharm/django_test_manage.py test --keepdb content.tests.ContentTest /opt/project/
At the same time, the tests were run only once if you went into the container and ran the tests with the usual command:
python manage.py test
I have 2 versions of the
docker-compose.yml file: one for development and the other for production:
It turned out that it was necessary to remove the
restart: unless-stopped setting in the
docker-compose.dev.yml file for development, so that the restart mode configuration for the service would be the default, that is:
restart: 'no'. In general, during development there is no need to restart containers in case of failure. On the contrary, if some kind of failure occurs, you can see it by the idle container and not miss the problem that arises (but in the production version of
docker-compose.prod.yml it is usually good to have the
restart: unless-stopped setting so that the container is automatically started when some kind of failure). The service in
docker-compose.yml might look something like this:
version: '3.8' services: core: container_name: spez_core_dev build: context: . dockerfile: dev.Dockerfile command: python manage.py runserver 0.0.0.0:8000 restart: 'no' # dev server no need to restart automatically (it excludes double run testing) ...
The reason for this behavior is still not completely clear to me: either when running the test, the container stopped and started again for some reason, or docker itself thinks that the container has fallen off and a new one needs to be created and started, or maybe the reason is something else ...