Pytest 在 pre-commit 中运行两次。

huangapple go评论60阅读模式
英文:

Pytest runs twice in pre-commit

问题

似乎 pre-commit 会两次运行 pytest 测试。如何重现:

  1. 安装 pytest、pytest-cov 和 pre-commit。

  2. 添加 .pre-commit-config.yaml

    repos:
    - repo: local
      hooks:
        - id: unit_test
          name: 运行单元 pytest
          language: python
          language_version: python3
          entry: pytest tests/unit --cov=src/
          types: [ python ]
          fail_fast: true
          stages: [commit]
    
        - id: integr_tests
          name: 运行集成 pytest
          language: python
          language_version: python3
          entry: pytest tests/integration --cov=src/
          types: [ python ]
          fail_fast: true
          stages: [push]
    
  3. 安装 pre-commit:pre-commit install --hook-type pre-commit --hooktype pre-push

  4. 将测试添加到 tests/unit 文件夹。我的结构
    点击此处查看图像描述

  5. test_config_source.py 包含两个虚拟测试以失败:
    点击此处查看图像描述

  6. 手动运行 pre-commit pre-commit 导致测试失败两次:
    点击此处查看图像描述

  7. 从 pre-commit 分开运行相同命令 pytest tests/unit --cov=src/ 是正常的:
    点击此处查看图像描述

我做错了什么?

英文:

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?

答案1

得分: 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

英文:

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

huangapple
  • 本文由 发表于 2023年3月8日 19:35:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75672504.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定