• Stars
    star
    703
  • Rank 63,008 (Top 2 %)
  • Language
    Python
  • License
    MIT License
  • Created over 9 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

Local continuous test runner with pytest and watchdog.

pytest-watch -- Continuous pytest runner

Current version on PyPI Say Thanks!

pytest-watch a zero-config CLI tool that runs pytest, and re-runs it when a file in your project changes. It beeps on failures and can run arbitrary commands on each passing and failing test run.

Motivation

Whether or not you use the test-driven development method, running tests continuously is far more productive than waiting until you're finished programming to test your code. Additionally, manually running pytest each time you want to see if any tests were broken has more wait-time and cognitive overhead than merely listening for a notification. This could be a crucial difference when debugging a complex problem or on a tight deadline.

Installation

$ pip install pytest-watch

Usage

$ cd myproject
$ ptw
 * Watching /path/to/myproject

Note: It can also be run using its full name pytest-watch.

Now develop normally and check the terminal every now and then to see if any tests are broken. Alternatively, pytest-watch can notify you when tests pass or fail:

  • OSX

    $ ptw --onpass "say passed" --onfail "say failed"

    $ ptw --onpass "growlnotify -m \"All tests passed!\"" \
          --onfail "growlnotify -m \"Tests failed\""

    using GrowlNotify.

  • Windows

    > ptw --onfail flash

    using Console Flash

You can also run a command before the tests run, e.g. seeding your test database:

$ ptw --beforerun init_db.py

Or after they finish, e.g. deleting a sqlite file. Note that this script receives the exit code of pytest as an argument.

$ ptw --afterrun cleanup_db.py

You can also use a custom runner script for full pytest control:

$ ptw --runner "python custom_pytest_runner.py"

Here's an minimal runner script that runs pytest and prints its exit code:

# custom_pytest_runner.py

import sys
import pytest

print('pytest exited with code:', pytest.main(sys.argv[1:]))

Need to exclude directories from being observed or collected for tests?

$ ptw --ignore ./deep-directory --ignore ./integration_tests

See the full list of options:

$ ptw --help
Usage: ptw [options] [--ignore <dir>...] [<directory>...] [-- <pytest-args>...]

Options:
  --ignore <dir>        Ignore directory from being watched and during
                        collection (multi-allowed).
  --ext <exts>          Comma-separated list of file extensions that can
                        trigger a new test run when changed (default: .py).
                        Use --ext=* to allow any file (including .pyc).
  --config <file>       Load configuration from `file` instead of trying to
                        locate one of the implicit configuration files.
  -c --clear            Clear the screen before each run.
  -n --nobeep           Do not beep on failure.
  -w --wait             Waits for all tests to complete before re-running.
                        Otherwise, tests are interrupted on filesystem events.
  --beforerun <cmd>     Run arbitrary command before tests are run.
  --afterrun <cmd>      Run arbitrary command on completion or interruption.
                        The exit code of "pytest" is passed as an argument.
  --onpass <cmd>        Run arbitrary command on pass.
  --onfail <cmd>        Run arbitrary command on failure.
  --onexit <cmd>        Run arbitrary command when exiting pytest-watch.
  --runner <cmd>        Run a custom command instead of "pytest".
  --pdb                 Start the interactive Python debugger on errors.
                        This also enables --wait to prevent pdb interruption.
  --spool <delay>       Re-run after a delay (in milliseconds), allowing for
                        more file system events to queue up (default: 200 ms).
  -p --poll             Use polling instead of OS events (useful in VMs).
  -v --verbose          Increase verbosity of the output.
  -q --quiet            Decrease verbosity of the output (precedence over -v).
  -V --version          Print version and exit.
  -h --help             Print help and exit.

Configuration

CLI options can be added to a [pytest-watch] section in your pytest.ini file to persist them in your project. For example:

# pytest.ini

[pytest]
addopts = --maxfail=2


[pytest-watch]
ignore = ./integration-tests
nobeep = True

Alternatives

  • xdist offers the --looponfail (-f) option (and distributed testing options). This instead re-runs only those tests which have failed until you make them pass. This can be a speed advantage when trying to get all tests passing, but leaves out the discovery of new failures until then. It also drops the colors outputted by pytest, whereas pytest-watch doesn't.
  • Nosey is the original codebase this was forked from. Nosey runs nose instead of pytest.

Contributing

  1. Check the open issues or open a new issue to start a discussion around your feature idea or the bug you found
  2. Fork the repository, make your changes, and add yourself to Authors.md
  3. Send a pull request

If you want to edit the README, be sure to make your changes to README.md and run the following to regenerate the README.rst file:

$ pandoc -t rst -o README.rst README.md

If your PR has been waiting a while, feel free to ping me on Twitter.

Use this software often? Say Thanks! πŸ˜ƒ

More Repositories

1

grip

Preview GitHub README.md files locally before committing them.
Python
6,132
star
2

gitpress

Blissful blogging for hackers.
Python
79
star
3

inbox-by-gmail-checker

Displays the number of unread messages in Inbox by Gmail. You can also click the button to open your inbox.
JavaScript
69
star
4

py-getch

Portable getch() for Python.
Python
56
star
5

sublimetext-console-exec

Run a Sublime Text command in a console window. Supports both Sublime Text 2 & 3.
Python
36
star
6

django-q-email

Queues the sending of email with Django Q.
Python
33
star
7

flask-pytest

Runs pytest in a background process when DEBUG is True.
Python
28
star
8

hello-redis-tasks

An example of how to use Redis as a task queue within a Flask web application.
Python
26
star
9

window-finder-control

A .NET control that behaves like the Spy++ window finder tool, allowing the user to select a top-level window by dragging a crosshair icon over it.
C#
26
star
10

checkers

The game of Checkers, supporting single player with different difficulties, two player, and an online game with full hosting and chat support.
C#
23
star
11

upgrade-requirements.py

Upgrade all your outdated requirements.txt in a single command.
Python
14
star
12

gistmail

Get email summaries of websites.
Python
13
star
13

flask-s3-example

A quick example on how to directly upload to S3 using Flask.
JavaScript
13
star
14

autohotkey-scripts

My currently running AutoHotKey scripts.
AutoHotkey
12
star
15

flask-scaffold

A scaffold for quickly developing a Flask project on a production server using Passenger.
Python
11
star
16

py-impactjs

Implements a back-end server in Python for Impactjs that lets you develop multiple games at once. There's also a basic UI for browsing your games, creating new ones, and adding assets to your project.
Python
10
star
17

console-flash

A simple command-line application for Windows to flash a command window, letting you know when a script finishes. A common place to use this at the end of a batch file.
C#
10
star
18

django-werkzeug

Enhances Django runserver with Werkzeug.
Python
9
star
19

github-version-tags

Displays the latest version of a project next to the repo name on GitHub.
JavaScript
8
star
20

webcam-lib

A .NET library that provides a simplified API to interact with a webcam device.
C#
7
star
21

gesso.js

A framework and CLI tool for rapidly creating HTML <canvas> projects.
JavaScript
7
star
22

tabhouse

A web application that lets you find quality guitar tabs.
Python
7
star
23

minesweeper

My first Flash game written in ActionScript 3.
ActionScript
6
star
24

glance.py

Highlight stack traces so you can glance at your terminal instead of studying it.
Python
4
star
25

tray-me

Add any Windows application to the tray area.
C#
4
star
26

hack-function

"Goto Definition" for the Python Interpreter
Python
4
star
27

nosey

Local continuous test runner with nose and watchdog.
Python
4
star
28

google-apps-client

Combines the ease and familiarity of Outlook's notifications with the power of Google Apps.
C#
4
star
29

study-guide

A fun, high-tech alternative to flashcards [2004]
C#
3
star
30

jsonpointer-cli

CLI for retrieving a specific value within a JSON document.
JavaScript
3
star
31

snap-it

Allows you to take snapshots from the webcam at set intervals for creating time-lapse videos.
C#
3
star
32

path-and-address

Functions for server CLI applications used by humans.
Python
3
star
33

facebook-photobook

A tool that allows you to download your photos from Facebook, including any photos you've been tagged in.
C#
3
star
34

mockdown

Add basic interactivity to your mockups for demos and developers.
Python
3
star
35

ghost-heroku-kit

Run Ghost on Heroku with as little effort as possible.
CSS
2
star
36

prepend

Prepends a list of files with text.
Python
2
star
37

gitpress-blog

Blissful blogging for hackers. The Blog.
2
star
38

todos-api

Example mobile TODO app.
Python
2
star
39

joeyespo.github.io

The content for joeyespo.com.
HTML
2
star
40

income-comparer

Quickly calculate and compare income.
HTML
2
star
41

guestbook-aggregator

An example website that aggregates guestbooks using a REST API.
Python
1
star
42

ituner

A utility that interfaces with iTunes to provide additional functionality.
C#
1
star
43

gmail-button-experiment

An experiment to help me be more productive in my day-to-day by checking email less often.
JavaScript
1
star
44

todos-app

Example mobile TODO app.
JavaScript
1
star
45

academica

JavaScript
1
star
46

sublimetext-settings

My Sublime Text 2 settings for syncing between platforms and devices.
1
star
47

drawing-visualizer

A Visual Studio visualizer to see images and bitmaps in variables while debugging code.
C#
1
star
48

import

Shorthand for typing 'python' then 'import some-module' in the terminal.
Batchfile
1
star
49

danespo

My brother's blog.
CSS
1
star
50

mvrnd

Move a file at random.
Python
1
star
51

rethinkhost.com

The website for RethinkHost.
JavaScript
1
star
52

flask-guestbook-api

This consumes both APIs demonstrated during the PghPy Flask API meetup.
Python
1
star
53

parkhillchiropracticcenter.com

The information website for Dr. Tad Parkhill.
HTML
1
star
54

iwidgets.aggrenda.com

Renders Aggrenda widgets as images.
JavaScript
1
star