I'd like to write a zsh completion for pytest.

Where do I Start? I'm using oh-my-zsh.

in .zshrc:

fpath=($HOME/.mycompletions $fpath)
autoload -U compinit && compinit -u

in $HOME/.mycompletions/_pytest:

#compdef pytest

    COMPREPLY=(`pytestcomplete ${cur} 2>/dev/null`)
complete -o nospace -F _pytest py.test

Is this correct so far?

Now I "only" need to write the pytestcomplete script.

What should the Return values Look like? How can I hand over to the script which parts have been completed?

I.e. if a user does py.test<TAB> it should complete files first. If one does py.test tests/my.test.py<TAB> it should complete class names. If one does py.test tests/my.test.py::TestClass<TAB> it should complete method names.

To get the info out of pytest, one would use --collect-only. The only problem at the moment is the back and forth between zsh and the completion script.

And can this be done like this or do I need to write a oh-my-zsh plugin?


1 Answer 1


You don't need to write your own completion function for that. You can simply re-use pytest's Bash completion:

  1. Install argcomplete:
    pip install argcomplete
  2. Add the following to your .zshrc:
    autoload -Uz bashcompinit && bashcompinit
    eval "$(register-python-argcomplete pytest)"
  3. Restart your shell.
  4. Done!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .