/pytest-el

Run py.test on testing functions, classes, modules and entire suites in Emacs.

Primary LanguageEmacs Lisp

pytest.el

`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 (https://bitbucket.org/durin42/nosemacs). A special thanks to Jason Pellerin and Augie Fackler for writing nose.el.

Installation

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:

py.test --genscript=run-tests.py

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")

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

By default, the root of a project is found by looking for any of the files ‘setup.py’, ‘.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)))