0

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?

anthony sottile
  • 61,815
  • 15
  • 148
  • 207

1 Answers1

1

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

anthony sottile
  • 61,815
  • 15
  • 148
  • 207
  • Thanks Anthony. That helped. And yes, you're right, using pytest with a pre-commit in any CI-CD is not a good enough idea. But I'm not going to use it there. pre-commit is only used in the local development environment to force unit tests to run on any code commit/change and huge integration tests once per push event. This is done in conjunction with code linters and code analyzers. I would also like to thank you for your wonderful tool that helps us so much! – Alex Grand Mar 09 '23 at 09:12