Search code examples
pytestpre-commitpre-commit.com

Pytest runs twice in pre-commit


Seems like pre-commit runs pytest tests twice. How to reproduce:

  1. Install pytest, pytest-cov, pre-commit

  2. 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]
    
  3. Install pre-commit: pre-commit install --hook-type pre-commit --hooktype pre-push

  4. Add tests to tests/unit folder. My structure enter image description here

  5. test_config_source.py contains two dummy tests to fail: enter image description here

  6. Running pre-commit manually pre-commit leads to doubled failed tests: enter image description here

  7. Running same command separately from pre-commit pytest tests/unit --cov=src/ is fine: enter image description here

What am I doing wrong?


Solution

  • 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