python-pytest.el
is an emacs package to integrate the python pytest test runner.
most functionality can be used via a dispatcher popup menu built using transient, which gives a look and feel similar to the fantastic magit package.
python-pytest.el
offers these awesome features:
C-u
).Output -c color (--color) -q quiet (--quiet) -s no output capture (--capture=no) -v verbosity ([--verbose|--verbose --verbose]) Selection, filtering, ordering -k only names matching expression (-k=) --dm run doctests (--doctest-modules) -m only marks matching expression (-m=) --nf new first (--new-first) --sw stepwise (--stepwise) Failures, errors, debugging -l show locals (--showlocals) --ff failed first (--failed-first) -p debug on error (--pdb) --ft full tracebacks (--full-trace) -x exit after first failure (--exitfirst) --mf exit after N failures or errors (--maxfail=10) --rx run xfail tests (--runxfail) --tb traceback style (--tb=) --tr debug on each test (--trace) Run tests t all r repeat f file (dwim) m files d def/class (dwim) x last failed F file (this) M directories D def/class (this)
python-pytest.el
is available from melpa.
with use-package
:
(use-package python-pytest)
install manually:
M-x package-install RET python-pytest RET
note that python-pytest.el
uses projectile for some of its features, e.g. finding associated test files. this package is intended to work correctly even without any projectile
configuration, since it will likely do the right thing if a project has a conventional layout.
to run function/class tests, tree-sitter
is needed, including the python language grammar:
M-x treesit-install-language-grammar
the typical usage pattern is to invoke the popup menu, named python-pytest-dispatch
. it is a good idea to create a dedicated keybinding for this command, but it can also be run manually:
M-x python-pytest-dispatch
this shows a dispatcher menu. change some switches and options, then run one of the actions.
a dedicated pytest comint
buffer will open, showing the output in real time, and allowing interaction with debuggers.
this package ultimately invokes pytest
. python-pytest.el
does not guess execution environments, so emacs needs to use the right exec-path
, taking into account python virtual environments, and so on.
to manage the execution environment, consider using direnv: it can change (and revert) paths and environment variables, simply by switching to a project directory, making it perfect for automatically ‘activating’ a virtualenv
. use emacs-direnv and possibly exec-path-from-shell to achieve the same inside emacs.
by default, pytest
is run from the project root directory. if your package is not at the root of your repository, pytest
might not find your modules.
a workaround is to add the the package root to PYTHONPATH
before running the tests. this can be found by adding a dummy file in the package root. the following hook looks for a .pyroot
file in parent directories. if found, it adds the directory of the file to PYTHONPATH
.
(add-hook 'python-mode-hook (lambda () (when-let ((r (locate-dominating-file default-directory ".pyroot"))) (setq python-pytest-executable (concat "PYTHONPATH=" r " " "pytest")))))
to edit the command line before running it, use a prefix argument before calling the action, e.g.type C-u t
instead of just t
in the popup menu.
when the popup menu itself is invoked with a prefix argument, this will run python-pytest-repeat
to rerun pytest. this means a single key binding can be used for both an initial run (via the popup), and for repeated calls. this is great for quick ‘edit, test, edit, test` cycles.
the available commands are:
python-pytest
python-pytest-file
python-pytest-file-dwim
python-pytest-files
python-pytest-function
python-pytest-function-dwim
python-pytest-last-failed
python-pytest-repeat
all of these are available via the popup menu, but can also be executed directly (or bound to a key).
this package uses a few heuristics for its ‘do what i mean’ behaviour.
the python-pytest-file-dwim
command tries to do the right thing both when editing the actual code and its associated test module. for instance, when editing foo/bar.py
, this will automatically detect tests/test_bar.py
(thanks to the projectile
package), and only run the tests from that test module.
the python-pytest-function-dwim
command tries to run only tests related to the function close to the cursor position (‘point’ in emacs terminology).
when editing a test module, this runs only a single test function, namely the one currently being edited.
when editing the code itself, things are more complicated. this command will make a guess to only run the right test functions. the matching behaviour can be tweaked using python-pytest-strict-test-name-matching
(see configuration below).
by default, the current function name will be used as a pattern to match the corresponding tests. for example, when editing foo()
inside utils.py
, this will match test_foo()
as well as test_foo_xyz()
, by invoking pytest test_utils.py -k test_foo
. if a pattern was specified in the popup (the -k
option), it will try to make a combined pattern, by invoking pytest test_utils.py -k 'test_foo and other_filter'
.
on the other hand, when python-pytest-strict-test-name-matching
is non-nil, only test_foo()
will match, and nothing else, by invoking pytest test_utils.py::test_foo
.
the behaviour of this package can be tweaked by customising a few defcustom variables. use the customize
interface to explore those (each will show a description and possible values):
M-x customize-group RET python-pytest RET
to set those permanently without using the customize interface, use something like this in init.el
:
(use-package python-pytest :custom (python-pytest-confirm t))
the available variables are:
python-pytest-confirm
whether to ask for confirmation (allowing editing) by default. this inverts the prefix argument (C-u
) behaviour.
python-pytest-strict-test-name-matching
Whether to require a strict match for the ‘test this function’ heuristic.
python-pytest-executable
the name of the pytest executable (pytest
by default)
python-pytest-unsaved-buffers-behavior
whether to ask whether unsaved buffers should be saved before running pytest. the check for unsaved buffers can be for only the current buffer, or for all project buffers, and those can be saved directly, or after confirmation. valid values: ask-all
, ask-current
, save-all
, save-current
, or nil
.
python-pytest-setup-hook
, python-pytest-started-hook
, and python-pytest-finished-hook
hooks run before starting pytest
, after starting pytest
, and after pytest
finished.
python-pytest-buffer-name
and python-pytest-project-name-in-buffer-name
the defaults result in *pytest*<project-name>
.
python-pytest-pdb-track
whether to enable the pdb tracking support
when using pytest plugins that provide extra switches, it may be useful to integrate those into the popup. see the transient manual for more information.
as an example, this will add a -z
switch that, when enabled, will invoke pytest --zzz
:
(use-package python-pytest :config ;; just an extra `-y' after the `-x' suffix (transient-append-suffix 'python-pytest-dispatch "-x" '("-y" "The Y" "-y")) ;; group with `-z' after second from the last group, ;; that is before `Run tests' (transient-append-suffix 'python-pytest-dispatch '(-2) ["My Z" ("-z" "The Z" "-z")]))
transient lets you save defaults you want for it. just select all options on python-pytest-dispatch
and then
C-x C-s
to save current settings as default and make them persistent,C-x s
to save current settings as default for the current emacs session.praise? complaints? bugs? questions? ideas?
please use the github issue tracker.
this package was created by wouter bolsterlee. i am @wbolster on github and twitter.
note: melpa automatically ships the latest code from the git main
branch, while melpa stable only contains tagged (released) versions.
tree-sitter
for function/class at point commands (#75)--log-cli-level
(#74)project.el
support (#73)completing-read-multiple
for multiple files selection (#72)projectile-compilation-dir
if it exists (#59)read-shell-command
instead of read-from-minibuffer
(#60)0
as a valid argument that can be passed to -n
(#61)compilation-mode
after pytest process finishes (#62)-
or --
(mostly mimicking pytest flags) (#28)python-pytest-directories
command with interactive multi-directory selection (#21, #31)transient
(magit-popup
replacement); the command for the menu is now python-pytest-dispatch
(#18, #26)python-pytest-files
command with interactive multi-file selectionpython-pytest-file-dwim
heuristic for nested functions/classesnext-error
and related-commands work-w
shortcut for very verbose (--verbose --verbose
) (#24)python-pytest-dispatch
from an output buffer. (#3)-as->
macro since the dash.el
version currently on melpa stable does not have it. (#2)python-pytest-popup
command(this is the osi approved 3-clause "new bsd license".)
copyright 2018 wouter bolsterlee
all rights reserved.
redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
this software is provided by the copyright holders and contributors "as is" and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. in no event shall the copyright holder or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4