gpt4 book ai didi

跳过 Python 预提交单元测试

转载 作者:行者123 更新时间:2023-12-01 06:37:53 30 4
gpt4 key购买 nike

我想使用pre-commit处理我的 git 项目的 git hooks。但是,当我使用它时,git commit 命令不断跳过 unittest 执行:

(smartexchange) trnbook:SmartExchange ale$ git commit -m "add pre-commit yaml config"
autopep8.............................................(no files to check)Skipped
unittest.............................................(no files to check)Skipped
[hook_precommit da26d1e] add pre-commit yaml config
1 file changed, 14 insertions(+)
create mode 100644 .pre-commit-config.yaml

预提交 Hook 手动执行的结果相同:

(smartexchange) trnbook:SmartExchange ale$ pre-commit install && python .git/hooks/pre-commit
pre-commit installed at .git/hooks/pre-commit
autopep8.............................................(no files to check)Skipped
unittest.............................................(no files to check)Skipped

我错过了什么?手动执行python -m unittest discovery没问题,执行了4个unittest:

(smartexchange) trnbook:SmartExchange ale$ python -m unittest discover -s smartexchange/
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

我已经阅读了预提交用户文档和这个答案:

Python pre-commit unittest faild

这是我的 .pre-commit-config.yaml 文件。

repos:
- repo: https://github.com/pre-commit/mirrors-autopep8
rev: '' # Use the sha / tag you want to point at
hooks:
- id: autopep8
- repo: local
hooks:
- id: unittest
name: unittest
entry: python -m unittest discover
language: python
'types': [python]
additional_dependencies: []
pass_filenames: false

我使用miniconda作为环境经理。这是我的 conda list 输出:

(smartexchange) trnbook:SmartExchange ale$ conda list
# packages in environment at /Users/ale/bin/miniconda3/envs/smartexchange:
#
# Name Version Build Channel
aspy.yaml 1.3.0 py_0 conda-forge
ca-certificates 2019.11.27 0
cached-property 1.5.1 py_1
certifi 2019.11.28 py37_0
cfgv 2.0.1 py_0 conda-forge
editdistance 0.5.3 py37h0a44026_0 conda-forge
identify 1.4.9 py_0 conda-forge
importlib_metadata 1.3.0 py37_0
libcxx 4.0.1 hcfea43d_1
libcxxabi 4.0.1 hcfea43d_1
libedit 3.1.20181209 hb402a30_0
libffi 3.2.1 h475c297_4
more-itertools 8.0.2 py_0
ncurses 6.1 h0a44026_1
nodeenv 1.3.3 py_0 conda-forge
openssl 1.1.1d h1de35cc_3
pip 19.3.1 py37_0
pre-commit 1.21.0 py37_0 conda-forge
python 3.7.5 h359304d_0
pyyaml 5.2 py37h1de35cc_0
readline 7.0 h1de35cc_5
setuptools 42.0.2 py37_0
six 1.13.0 py37_0
sqlite 3.30.1 ha441bb4_0
tk 8.6.8 ha441bb4_0
toml 0.10.0 py37h28b3542_0
virtualenv 16.7.5 py_0
wheel 0.33.6 py37_0
xz 5.2.4 h1de35cc_4
yaml 0.1.7 hc338f04_2
zipp 0.6.0 py_0
zlib 1.2.11 h1de35cc_3

我使用的操作系统是MacOS Catalina,版本10.15.2。

<小时/>

编辑:

Anthony's answer很好;不过,为了将来引用,最好报告我用来运行 unittest 命令(此处 docs about test-discovery )和选项 --start-directory=path/to/python_module_folder 的修改后的配置:

    -   id: unittest
name: unittest
entry: python -m unittest discover
language: python
types: [python]
args: [--start-directory=path/to/python_module_folder, --pattern=test_*.py]
pass_filenames: false
verbose: true

pre-commit documentation about argument pattern in hooks 报道,其他参数应位于 long format 中.

最佳答案

pre-commit只会运行 files which are staged 的钩子(Hook)在您的特定提交中

此配置:

    -   id: unittest
name: unittest
entry: python -m unittest discover
language: python
'types': [python]
additional_dependencies: []
pass_filenames: false

仅在 types: [python] 时运行文件匹配(对于 git commit 这意味着需要更改 python 文件才能执行)

如果您希望它始终运行,可以使用 always_run: true (不过,您可能可以节省一些时间)

如果您有任何 python 文件(将由 types: [python] 匹配),您还可以使用 pre-commit run --all-files 来触发它

<小时/>

一些额外的提示

  • 您不需要引用'types' (可以只使用types,因为yaml支持裸词)
  • additional_dependencies: []是默认的,您可以删除此行
<小时/>

免责声明:我是 pre-commit 的作者

关于跳过 Python 预提交单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59586334/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com