1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
|
# Welcome to pytest-watcher
[PyPI - Python Version](https://pypi.org/project/pytest-watcher/)
[GitHub](https://github.com/olzhasar/pytest-watcher/blob/master/LICENSE)
## Overview
**pytest-watcher** is a tool to automatically rerun tests (using `pytest` by default) whenever your code changes.
Works on Unix (Linux, MacOS, BSD) and Windows.
Example:

## Motivation
### Why not general tools
- Easy to use and remember
- Works for most Python projects out of the box
- Uses native system monitoring API instead of polling on supported systems (see [watchdog documentation](https://python-watchdog.readthedocs.io/en/stable/installation.html#supported-platforms-and-caveats))
- Listens for new file, delete file, change and move events
- Runs your tests with latest changes in case of post-processing events (see [delay](#delay))
- Has interactive mode with handy keyboard shortcuts
### What about pytest-watch
[pytest-watch](https://github.com/joeyespo/pytest-watch) has been around for a long time and used to address exactly this problem. Unfortunately, pytest-watch is no longer maintained and doesn't work for many users. This project provides an alternative for it.
See also: [Differences with pytest-watch](#differences-with-pytest-watch)
## File events
By default `pytest-watcher` looks for the following events:
- New `*.py` file created
- Existing `*.py` file modified
- Existing `*.py` file deleted
- A `*.py` file moved either from or to the watched path
You can specify alternative file patterns to watch. See [Watching different patterns](#watching-different-patterns)
## Installation
```sh
pip install pytest-watcher
```
## Usage
Specify the path that you want to watch:
```sh
ptw .
```
or
```sh
ptw /home/repos/project
```
`pytest-watcher` will pass any arguments (excepted [reserved options](#available-options)) after `<path>` to the test runner (which is `pytest` by default). For example:
```sh
ptw . -x --lf --nf
```
will call `pytest` with the following arguments:
```sh
pytest -x --lf --nf
```
### Available options
The following options are reserved for `pytest-watcher` and will not be passed to the test runner:
- `--runner` - Specify an alternative test runner
- `--patterns` - Specify file patterns to watch
- `--ignore-patterns` - Specify file patterns to ignore
- `--now` - Run tests immediately after starting the watcher
- `--delay` - Specify the delay before running tests
- `--clear` - Clear the terminal screen before each test run
### Using a different test runner
You can specify an alternative test runner using the `--runner` flag:
```sh
ptw . --runner tox
```
### Watching different patterns
You can use the `--patterns` flag to specify file patterns that you want to watch. It accepts a list of Unix-style patterns separated by a comma. The default value is "\*.py"
Example:
```sh
ptw . --patterns '*.py,pyproject.toml'
```
You can also **ignore** certain patterns using the `--ignore-patterns` flag:
```sh
ptw . --ignore-patterns 'settings.py,db.py'
```
### Delay
`pytest-watcher` uses a short delay (0.2 seconds by default) before triggering the actual test run. The main motivation for this is post-processors that can run after you save the file (for example, `black` plugin in your IDE). This ensures that tests will run with the latest version of your code.
You can control the actual delay value with the `--delay` flag:
```sh
ptw . --delay 0.2
```
To disable the delay altogether, you can set zero as a value:
```sh
ptw . --delay 0
```
### Screen clearing
Use the `--clear` flag to clear the terminal screen before each test run
```sh
ptw . --clear
```
### Differences with `pytest-watch`
Even though this project was inspired by [`pytest-watch`](https://github.com/joeyespo/pytest-watch), it's not a fork of it. Therefore, there are **differences** in behavior:
- `pytest-watcher` needs you to specify a path to watch as a first argument:
```sh
ptw .
```
- `pytest-watcher` doesn't start tests immediately by default. You can customize this behavior using `--now` flag.
## Configuring
You can configure `pytest-watcher` via `pyproject.toml` file. Here is the default configuration:
```toml
[tool.pytest-watcher]
now = false
clear = true
delay = 0.2
runner = "pytest"
runner_args = []
patterns = ["*.py"]
ignore_patterns = []
```
## Compatibility
The code is compatible with Python versions 3.7+
|