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:

my_app/devops/docker-compose.dev.yml
my_app/devops/docker-compose.prod.yml

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 ...

Rate this article

0 from 5 (total 0 ratings)

You can send feedback, suggestions or comments on this article using this form:

Fields marked by star ( * ) is required.

Thank you for yor feedback!

After clicking the "Send" button, your message will be delivered to me on the mail.

Author of Article

Artem Maltsev

Web-developer, having the knowlenge of programming language - Python, framework - Django, content management system - Django CMS, platform of e-commerce site - Django Shop and many other applications, using this technologies.

The right to use content on this page https://vivazzi.pro/it/how-to-fix-double-run-testing-with-docker-in-django/:

Permission is granted to copy an content with its author and reference to the original without using the parameter rel="nofollow" in tag <a>. Usage:

Author of Article: Artem Maltsev
Link to article: <a href="https://vivazzi.pro/it/how-to-fix-double-run-testing-with-docker-in-django/">https://vivazzi.pro/it/how-to-fix-double-run-testing-with-docker-in-django/</a>

More: Terms of site usage

Comments: 0

You can leave a comment as an unregistered user.

But if you sing up, you can:

  • receive notifications
  • view your comments
  • be able to use all the functions of the developed services

To comment in one's own name you should log in or sign up on Vuspace website

Send

There is no search on this site, so I offer to use usual search engine, for example, Google, adding "vivazzi" after your request.

Try it

Select currency for displaying monetary values