Seems like pre-commit runs pytest tests twice. How to reproduce:
Install pytest, pytest-cov, pre-commit
Add .pre-commit-config.yaml
:
repos:
- repo: local
hooks:
- id: unit_test
name: Run unit pytest
language: python
language_version: python3
entry: pytest tests/unit --cov=src/
types: [ python ]
fail_fast: true
stages: [commit]
- id: integr_tests
name: Run integration pytest
language: python
language_version: python3
entry: pytest tests/integration --cov=src/
types: [ python ]
fail_fast: true
stages: [push]
Install pre-commit: pre-commit install --hook-type pre-commit --hooktype pre-push
Add tests to tests/unit
folder. My structure
enter image description here
test_config_source.py
contains two dummy tests to fail:
enter image description here
Running pre-commit manually pre-commit
leads to doubled failed tests:
enter image description here
Running same command separately from pre-commit pytest tests/unit --cov=src/
is fine:
enter image description here
What am I doing wrong?
to start, running tests as part of pre-commit is a bad idea -- it's too slow and your contributors will get frustrated and turn the whole thing off
pre-commit is not really designed to run tests -- which is part of why you're running into issues with it.
pre-commit is designed to run things by passing modified files as positional arguments to the commands you configure. you need to fight the framework here and turn that off
you can do that with the combination of pass_filenames: false
, and always_run: true
but again, you shouldn't run your tests as part of pre-commit
disclaimer: I wrote pre-commit