Python 已跳过预提交单元测试

Python pre-commit unittest skipped

我想使用 pre-commit 来处理我的 git 项目的 git 挂钩。但是,当我使用它时,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

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

(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 discover没问题,执行了4个unittest:

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

OK

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

这是我的 .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。


编辑:

is good; however it's best, for future reference, to report the revised configuration I use to run the unittest command (here 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, additional arguments should be in long format报道。

pre-commit 只会 运行 在您的特定提交

中挂钩 files which are staged

此配置:

    -   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

的作者