I'm using s.el, so install it.
(use-package s :ensure t)
(require 's)
(defun our-django--get-project-root-directory ()
"Get the root directory of your Django project"
(locate-dominating-file (or buffer-file-name default-directory)
"manage.py"))
(defun our-django--generate-command (command &optional options)
"Generate Django commands"
(format "python manage.py %s %s"
command (or options "")))
(defun our-django--get-run-test-command (file-name current-directory)
"Generate Django test run commands"
(our-django--generate-command
"test --verbosity 3 --keepdb" (s-replace "/" "."
(string-trim-right
(file-relative-name file-name current-directory)
".py"))))
(defun our-django--get-current-test-target ()
"Get the path from the current buffer to the test execution target"
(s-replace-regexp "/$" ""
(if buffer-file-name
;; file
(if (s-matches? "^test" (file-name-nondirectory buffer-file-name))
buffer-file-name
(file-name-directory buffer-file-name))
;; directory
default-directory)))
(defun our-django-test-single ()
"Run Django tests"
(interactive)
(let ((default-directory (our-django--get-project-root-directory)))
(our-async-exec
(our-django--get-run-test-command
(our-django--get-current-test-target)
default-directory))))
(defun our-django-load-fixture (fixture)
"Load Django Fixture"
(interactive "MFixture: ")
(let ((cmd (our-django--generate-command "loaddata" fixture))
(default-directory (our-django--get-project-root-directory)))
(async-shell-command cmd)))
(defun our-django-shell ()
"Run a Django shell"
(interactive)
(let ((cmd (our-django--generate-command "shell"))
(default-directory (our-django--get-project-root-directory)))
(async-shell-command cmd)))
(defun our-django-make-migrations (&optional app_label)
"Run Django's make migrations"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "makemigrations" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun our-django-migrate (&optional app_label)
"Run Django's migrate"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "migrate" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun our-django-show-migrations (&optional app_label)
"Run Django's show migrations"
(interactive "Mapp_label: ")
(let ((cmd (our-django--generate-command "showmigrations" app_label))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(defun nour-django-doctest ()
"Run Django's doctest"
(interactive)
(let ((default-directory (our-django--get-project-root-directory)))
(our-async-exec "python run_doctest.py")))
(defun our-django-squash-migrations (&optional app_label start_migration_name migration_name)
"Run Django's squash migrations"
(interactive (list
(read-from-minibuffer "app_abel: ")
(read-from-minibuffer "start_migration_name: ")
(read-from-minibuffer "migration_name: ")))
(let ((cmd (our-django--generate-command "showmigrations"
(s-join " "
`(,app_label
,start_migration_name
,migration_name))))
(default-directory (our-django--get-project-root-directory)))
(our-async-exec cmd)))
(bind-key* "C-t C-f" 'our-django-load-fixture)
(bind-key* "C-t C-r" 'our-django-test-single)
(bind-key* "C-t C-s" 'our-django-shell)
(bind-key* "C-t C-d C-c" 'our-django-make-migrations)
(bind-key* "C-t C-d C-e" 'our-django-migrate)
(bind-key* "C-t C-d C-l" 'our-django-show-migrations)
Open the file where the test is written and run it with M-x our-django-test-single
.
If you want to key bind, it looks like this.