英文:
Pytest runs twice in pre-commit
问题
似乎 pre-commit 会两次运行 pytest 测试。如何重现:
-
安装 pytest、pytest-cov 和 pre-commit。
-
添加
.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]
-
安装 pre-commit:
pre-commit install --hook-type pre-commit --hooktype pre-push
-
将测试添加到
tests/unit
文件夹。我的结构
点击此处查看图像描述 -
test_config_source.py
包含两个虚拟测试以失败:
点击此处查看图像描述 -
手动运行 pre-commit
pre-commit
导致测试失败两次:
点击此处查看图像描述 -
从 pre-commit 分开运行相同命令
pytest tests/unit --cov=src/
是正常的:
点击此处查看图像描述
我做错了什么?
英文:
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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论