Skip to content
Run py.test on testing functions, classes, modules and entire suites in Emacs.
Emacs Lisp
Branch: master
Clone or download
Latest commit 1bfa754 Oct 5, 2018
Type Name Latest commit message Commit time
Failed to load latest commit information. Change the README to an org file. Jun 18, 2015
pytest.el Find async test functions Oct 5, 2018


`pytest.el` provides a set of functions that handle running pytest on a particular buffer or part of a buffer. This started as a direct port of nosemacs ( A special thanks to Jason Pellerin and Augie Fackler for writing nose.el.


In your Emacs config:

(require 'pytest)

If you don’t use a global installation of py.test (ie in virtualenv) then add something like the following that points to either the non-global version or a test runner script.

(add-to-list 'pytest-project-names "my/crazy/runner")

For example, you can generate a script with py.test:


A much better pattern is to use a `.dir-locals.el` file to define the test runner executable. For example, I use a small library called xe for finding the current project’s virtualenv. Here is what `.dir-locals.el` would look like, using xe.

;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

  (pytest-global-name . "xe test")
  (pytest-cmd-flags . "")))

By default, the root of a project is found by looking for any of the files ‘’, ‘.hg’ and ‘.git’. You can add files to check for to the file list:

(add-to-list 'pytest-project-root-files ".bzr")

You can also change the project root test to detect in some other way whether a directory is the project root:

(setq pytest-project-root-test (lambda (dirname) (equal dirname "foo")))

Key bindings

Here are some suggested keybindings for running the tests within your python-mode buffers.

(add-hook 'python-mode-hook
          (lambda ()
            (local-set-key "\C-ca" 'pytest-all)
            (local-set-key "\C-cm" 'pytest-module)
            (local-set-key "\C-c." 'pytest-one)
            (local-set-key "\C-cd" 'pytest-directory)
            (local-set-key "\C-cpa" 'pytest-pdb-all)
            (local-set-key "\C-cpm" 'pytest-pdb-module)
            (local-set-key "\C-cp." 'pytest-pdb-one)))
You can’t perform that action at this time.