【问题标题】:Python pre-commit unittest skipped跳过 Python 预提交单元测试
【发布时间】:2020-01-03 23:00:05
【问题描述】:

我想使用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就ok了,执行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 中。

【问题讨论】:

    标签: python git python-unittest githooks pre-commit.com


    【解决方案1】:

    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的作者

    【讨论】:

      猜你喜欢
      • 2020-04-09
      • 1970-01-01
      • 2013-05-31
      • 2014-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多