A simple pre-commit hook to run tests with pytest.
Add this to your local .pre-commit-config.yaml
:
# .pre-commit-config.yaml
- repo: https://github.com/christophmeissner/pytest-pre-commit
rev: 1.0.0
hooks:
- id: pytest
pass_filenames: false
always_run: true
To run the hook without commiting, run...
$ pre-commit run pytest -a
When the tests pass, this won't produce any further output.
If you want to see more verbose output, run...
$ pre-commit run pytest -a -v
You can pass arguments to the invocation of pytest
by adding an args
list to the hook config in .pre-commit-config.yaml
, for example:
# .pre-commit-config.yaml
- repo: https://github.com/christophmeissner/pytest-pre-commit
rev: 1.0.0
hooks:
- id: pytest
pass_filenames: false
always_run: true
args:
- "--durations=0"
- "--last-failed"
- "--last-failed-no-failures=all"
- "--new-first"
- "-rfEsxXp"
- "-vvv"
... and/or you can add the pytest arguments for every test run in your pytest.ini
file, for example like this:
# pytest.ini
[pytest]
minversion = 6.0
addopts = --durations=0 --last-failed --last-failed-no-failures all --new-first -rfEsxXp -vvv
testpaths =
tests
For more information about pytest
CLI arguments and how to invoke pytest
, see pytest: How to invoke pytest and pytest: Complete pytest command-line flag reference.