• Stars
    star
    274
  • Rank 150,274 (Top 3 %)
  • Language
    Vim Script
  • Created almost 14 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Runs your UnitTests with py.test displaying red/green bars and errors

pytest.vim

follow @alfredodeza for updates

A simple way of running your tests (with py.test) from within VIM.

Talking with Holger Krekel (original author of py.test and vim user) we thought it would be neat to be able to call it from vim and get some immediate results.

This is especially useful when you are tweaking and do not want to be moving around between the terminal and your vim session.

Showing a Session

https://github.com/alfredodeza/pytest.vim/raw/master/extras/session.png

Fail tests and Errors

https://github.com/alfredodeza/pytest.vim/raw/master/extras/fails.png

Installation

This plugin is a file type plugin, which means that it is only enabled when the matching file type (Python in this case) is being edited. To ensure the plugin works correctly, the following setting must be enabled (usually in .vimrc):

filetype on

Without any install frameworks, all is needed is to drop the pytest.vim file in one of the Vim runtime paths, in the subdirectory ftplugin/python/

If using vim-pathogen the whole repository can be placed in .vim/bundle. Otherwise please follow the guidelines of the package manager of choice.

Usage

This plugin provides a single command:

Pytest

All arguments are able to be tab-completed. To ensure the plugin will be loaded, these settings must be enabled:

:set filetype on
:set filetype plugin on

Python Objects

For running tests the plugin provides 4 arguments with an optional one. These arguments are:

class
method
function
file
project

As you may expect, those will focus on the tests for the current class, method, function, the file or project.

If you are in a class and want to run all the tests for that class, you would call this plugin like:

:Pytest class

Whenever a command is triggered a small message displays informing you that the plugin is running a certain action. In the above call, you would see something like this:

Running tests for class TestMyClass

If you would like to see the complete py.test output you can add an optional verbose flag to any of the commands for Pytest. For the previous command, it would look like:

:Pytest class verbose

This would open a split scratch buffer that you can fully interact with. You can close this buffer with ':wq' or you can hit 'q' at any moment in that buffer to close it.

When tests are successful a green bar appears. If you have any number of fails you get a red bar with a line-by-line list of line numbers and errors.

I strongly encourage a mapping for the above actions. For example, if you wanted leader (the leader key is '' by default) mappings you would probably do them like this:

" Pytest
nmap <silent><Leader>f <Esc>:Pytest file<CR>
nmap <silent><Leader>c <Esc>:Pytest class<CR>
nmap <silent><Leader>m <Esc>:Pytest method<CR>

If you are working on a project, this plugin provides a way to run tests from anywhere within the project tree like this:

" Tests are in /path/to/project/tests/
" Working on   /path/to/project/module/file.py
:Pytest project

This would run all of the project tests (in /path/to/project/tests/) related to the active project. This works with a directory called "tests" or a file called "tests.py". It should be noted that this plugin searches upward through the directory tree, taking the first entry it finds. For example:

" Working on /home/project/file.py
/home/tests/          " This set of tests will not be run
/home/project/tests/  " This set of tests will be run

It is easy to check which set of tests will be run (the project test working directory):

:Pytest projecttestwd

Errors and Fails

This plugin also provides a way to jump to the actual error. Since errors can be living in a file other than your test (e.g. a syntax error in your source that triggers an assertion errro in the current file) you can also jump to that file. The list of jumping-to-error arguments are:

first
last
next
previous
end

Pytest DOES NOT JUMP AUTOMATICALLY to errors. You have to call the action. When you call a jump, a split buffer is opened with a file (if it is not the same as the one you are currently editing) and places you in the same line number were the error was reported.

If an error starts in the current file but ends on a different one, you can call that end of error by calling :Pytest end.

Output

Finally, you can also display in a split scratch buffer either the last list of failed tests (with line numbers, errors and paths) or the last py.test session (similar to what you would see in a terminal). The arguments that you would need to provide for such actions are:

session
fails

session is the buffer with a similar output to the terminal (but with syntax highlighting) and fails has the list of last fails with the exceptions.

If you are looking for the actual error, we have stripped it from the normal reporting but you can call it at any time with:

:Pytest error

The reason behind this is that as soon as you hit any key, the quick display goes away. With a split buffer you are in control and you can quit that window when you decide - while you work on fixing errors.

The commands that open the last session and the last fails are toggable: they will close the scratch buffer if it is open or will open it if its closed.

PDB

If you have ever needed to get into a pdb session and debug your code, you already know that it is a horrible experience to be jumping between Vim and the terminal. pytest.vim now includes a way of calling it with 2 options that will let you drop to a shell (inside Vim!) and control your pdb session.

py.test pdb on fail

Use this option when you need to use the built-in pdb support from py.test (e.g. drop to pdb when a test fails).

:Pytest class --pdb

The above command shows class but you can use this with all the objects supported (class, method , function and file).

py.test no capture

If you are placing import pdb; pdb.set_trace() somewhere in your code and you want to drop to pdb when that code gets executed, then you need to pass in the no-capture flag:

:Pytest class -s

Again the above command shows class but you can use this with all the objects supported (class, method, function and file).

Shell Support

This plugin provides a way to have a better shell experience when running verbose or pdb flags by using native Vim (only available with Vim 8 and newer).

looponfail

This is an extra option that will allow you to loop (run again) on fail. If the test fails, then this option will make Vim run the same test again as soon as the file is written.

Once the test passes, it will no longer re-run the tests again. This option is available for class, method, function and file. You would call it like:

:Pytest method looponfail

clear

If for some reason you need to reset and clear all global variables that affect the plugin you can do so by running the following command:

:Pytest clear

This is specifically useful when looponfail has been enabled and you want to stop its automatic behavior. Remember that looponfail will run every time you write the buffer and will keep doing so unless your test passes.

Fast Next/Previous Error

Now when the Failed Error list is open and it as focus (cursor is currently in that window) you can move to the next or previous failed test line by using the arrow keys, j/k or Ctrl-n / Ctrl-p

Whenever you hit the bottom or the top of the list, you can loop around it!

If you hit an error that displays not the previous window (e.g. your test file) then a message will state that it is skipping.

neovim support

There is full support for neovim. Tests will never block and will be completely asynchronous. When the test run ends the familiar green (or red) bar will be displayed.

Some changes where made as well to support interactive terminal sessions (when using -s and --pdb for example) to make use of the terminal support from neovim.

warning: When calling a test, the user needs to wait until that test ends before calling another test, otherwise, the plugin will kill the first in order to call the last one.

Configuration

Custom executable

By default, the plugin uses py.test as the executable to run tests. Some Linux distros mangle the name to provide both Python 3 and Python 2 variants which forces one to pick a different name for the executable.

This can be customized with either the filename of the executable or the path to the executable needed. For example, for a py.test-3 name, it could be set in this way:

let g:pytest_executable = "py.test-3"

Test directory

By default the project test directory is tests (i.e. test files are assumed to be in /path/to/project/test). The global variable pytest_test_dir may be used to change this, for example:

let g:pytest_test_dir = 'test_suite'

configures the test directory to be /path/to/project/test_suite

Test file

By default the test file is tests.py.The global variable pytest_test_file may be used to change this, for example:

let g:pytest_test_file = 'test_myproj.py'

configures the test file to be /path/to/project/tests/test_myproj.py (assuming the default value for the project test directory)

License

MIT Copyright (c) 2011-2015 Alfredo Deza <alfredo [at] deza [dot] pe>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

More Repositories

1

khuno.vim

A Python Flakes plugin
Vim Script
91
star
2

coveragepy.vim

A Vim plugin to facilitate integration with Coverage.py
Vim Script
90
star
3

rust-fundamentals

Rust Bootcamp Week 2: Rust fundamentals
68
star
4

konira

beautiful (readable) testing in Python
Python
60
star
5

posero.vim

Presentation of text or terminal action in Vim
Vim Script
53
star
6

chapa.vim

VIM plugin to move (or visually select) the next/previous class, method or function
Vim Script
37
star
7

jacinto.vim

Format and validate JSON files
Vim Script
29
star
8

python-and-rust-tools

Introduction to Command-line tools with Python and Rust
27
star
9

ai-fundamentals

Study notes for the Azure AI Fundamentals
27
star
10

remoto

Remote commands (or processes calls) in Python.
Python
26
star
11

rust-setup

Rust Bootcamp week 1: Set up you Rust development environment
23
star
12

dotfiles

Vim Script
20
star
13

huggingface-ghcr

MLOps packaging: build and push to GitHub Container Registry
Dockerfile
19
star
14

python-bootcamp-duke

Official syllabus for AIPI 503 - Python Bootcamp for Data and AI
18
star
15

az-900-23

Revised Study Notes for the AZ-900 Certification 2022-2023
16
star
16

jformat

A barebones Python repo for teaching purposes
Python
15
star
17

zsh-plugins

A collection of (sourceable) zsh plugins
Shell
15
star
18

mlflow-demo

Sample MLFlow projects
Python
14
star
19

huggingface-onnx

Example notebook to work with ONNX using Hugging Face libraries
Jupyter Notebook
12
star
20

huggingface-deploy-azure

Deploy a HuggingFace container on Azure Container Apps
Python
12
star
21

functional-pytest

Examples for functional testing with Pytest
Python
10
star
22

supay

A daemon library for Python scripts
Python
10
star
23

notario

Validation of Python dictionaries
Python
9
star
24

rust-azure-function

Continuous deployment to Azure Functions using Rust
Rust
9
star
25

cora

Vim virtual environments
Shell
8
star
26

rust-distroless-azure

Deploy a distroless Rust API to Azure
Rust
8
star
27

mapping-data

Coursera Lesson 2: Mapping Data to Python
Jupyter Notebook
8
star
28

flask-roberta

Sentiment Analysis with RoBERTa
Python
8
star
29

python-cli-tools-course

Python Command Line Tools course
Python
7
star
30

huggingface-docker

MLOps packaging of HuggingFace
Python
6
star
31

flask-app

A demo flask-app with Github-actions
Python
6
star
32

scraping-demo

Demo for scraping using scrapy
HTML
6
star
33

azure-fundamentals

Study guide for the Azure Fundamentals Certification
6
star
34

rust-cli-example

A small Rust CLI example you can use to build on
Rust
6
star
35

ceph-ansible

Ansible Playbooks for Ceph
Ruby
6
star
36

advanced-rust-cli

Create advanced Rust CLIs using examples
Rust
5
star
37

try-huggingface

Examples to try HuggingFace datasets and transformers
Jupyter Notebook
5
star
38

az-900-reference

Reference to pass the Azure fundamentals test (AZ-900 )
5
star
39

fastapi-onnx

FastAPI example project to do fast ML predictions over HTTP
Python
5
star
40

advanced-python-cli

Create advanced Python CLIs using examples
Python
5
star
41

ffsync-ansible

Ansible playbook for the Firefox Sync Server
Shell
5
star
42

applied-rust

Rust Bootcamp Week 4: Applying Rust to build a library
5
star
43

python-scripting

Coursera Week 2: Python scripting and SQL
Python
5
star
44

pytest-konira

A pytest plugin to run Konira DSL tests
Python
5
star
45

python-action

Run Python scripts on GitHub Actions
4
star
46

coima

plain, simple template engine for text files
Python
4
star
47

Presentations

Text based Presentations
4
star
48

argparse-python-cli

A very basic Python CLI tool without dependencies using the argparse module
Python
4
star
49

rust-template

A simple Rust template to help you get started with a Rust project
Makefile
4
star
50

vscode-mysql

Coursera week 4: work with VSCode and MySQL
4
star
51

copilot-snake

A demo to showcase how Copilot can help you build a Snake game
Rust
4
star
52

rust-structs-types-enums

Rust Bootcamp Week 3: Structs, Types, and Enums
4
star
53

python-testing

Python Bootcamp for Data week 3: Testing in Python with Pytest
Jupyter Notebook
4
star
54

python-cli

Build a Python command line tool from scratch
Python
3
star
55

azure-flask-container-app

Use Azure App Service to deploy a containerized Flask application
Python
3
star
56

pipeline-jobs

Shell
3
star
57

helga-karma

You're doing good work!
Python
3
star
58

static

A simple static HTML site
HTML
3
star
59

konira.vim

A vim plugin to run konira test cases
Vim Script
3
star
60

pecan-raw

A simple request context viewer for Pecan
Shell
3
star
61

huggingface-azure-acr

packaging for Azure and HuggingFace
Python
3
star
62

redactr

An example Microservice project with Rust for anonimizing personal data
Rust
2
star
63

ansible-jenkins

Ansible module to manage Jenkins
Python
2
star
64

delgado

Receive commands over a socket and execute them in a terminal
Python
2
star
65

helga-jenkins

A Jenkins plugin for helga
Python
2
star
66

azure-spot-runner

Bring up Azure VMs as hosted GitHub Action runners - programmatically
Shell
2
star
67

flask-ml-azure

Flask ML project using Azure pipelines
Python
2
star
68

repo-id

Get the ID of a GitHub repository
Rust
2
star
69

wari

A CLI OTP manager and utility
Python
2
star
70

create-a-vim-plugin

A very basic plugin in Vim
Vim Script
2
star
71

tambo

A command line dispatcher
Python
2
star
72

wikipedia-dataset

A demo on creating a dataset from wikipedia
Jupyter Notebook
2
star
73

plexer.vim

A multi-line multi-edit plugin for vim
Vim Script
2
star
74

pacha

Python
2
star
75

try-vscode-web

Examples for teaching Python using VSCode web
Jupyter Notebook
2
star
76

huggingface-acr

Use other container registries to automatically package a HuggingFace model
Python
2
star
77

ado-work-items

An Azure function to update DevOps work items
Python
2
star
78

helga-ansible

An ansible plugin for helgabot
Python
2
star
79

container-mlops-template

Learn how to create a FastAPI container with HuggingFace and deploy it to Azure
Python
2
star
80

guaman

PostgreSQL query analytics
Python
1
star
81

caral

A flexible PyPi mirror
Python
1
star
82

bondsman.vim

Customize Git branch information from Fugitive
Vim Script
1
star
83

ayni

A Sphinx document version handler
Python
1
star
84

potasio

A python version of tasseo: a live dashboard for Graphite
JavaScript
1
star
85

helga-yall

Thank you responses for the helga bot
Python
1
star
86

copilot-workshop

HTML
1
star
87

python-cli-example

A small Python CLI example that you can use to build on
Python
1
star
88

python-functions-and-classes

Python Bootcamp for Data week 2: Python Functions and Classes
Jupyter Notebook
1
star
89

elk-stack

A sample all-in-one ELK stack repo with Codespaces
1
star
90

ceph-jenkins-build

The set of build files for building everything Ceph.
1
star
91

click-python-cli

A very basic example of using the Click framework to create a Python CLI
1
star
92

distributed-tracing

Demo of Opentracing and Jaeger
Python
1
star
93

introduction-to-python

Python For Data bootcamp week 1: Introduction to Python
Jupyter Notebook
1
star
94

jenkins-for-devops

Jupyter Notebook
1
star
95

pyfmt

Format your python code
1
star
96

pytest-inobject

Find matches in objects and provide a diff
Python
1
star
97

huggingface-summarization

Summarization CLI tool using HuggingFace
Python
1
star
98

helga-excuses

Excuses plugin for helga
Python
1
star
99

rust-monitoring-logging

Rust for DevOps week 2: Examples and exercises for monitoring and logging in Rust
Rust
1
star
100

pecan-admin

An admin interface for the Pecan Framework
Python
1
star