Run django tests in docker environment from emacs using .dir-locals.el

Caveats:

  • you need the path to your project on the local machine to match the path to your project in your docker container, so if the projects lives at /usr/src/app in the container, your project locally has to live at /usr/src/app as well
  • relies on your project's test runner to be pytest for things like debugging. However, for running tests, you may be able to get away with adding (pytest-cmd-flags . "") to the list and removing the -- at the end of pytest-global-name's value to run all of your tests with the django test runner. I have not tested anything with django's default test runner yet
  • emacs doesn't play well with ipython, but the native completion features in emacs are usually good enough to compensate

.dir-locals.el

;; can use python-mode instead of nil, but i like being able to run these at any file
((nil . (
         (eval . (setq python-shell-interpreter "/usr/src/app/bin/run_shell.sh"))
         (pytest-global-name . "docker-compose exec django django-admin test --settings=config.settings.test --")
         ;; add this to remove args from the test-all command for pytest.el
         ;; (pytest-cmd-flags . "")
         )))

run_shell.sh

#!/bin/bash
docker exec -it app_django_1 django-admin shell_plus --plain
Note: The shebang is important! the command won't run properly from emacs without it