Python预提交单元测试失败

问题描述 投票:1回答:1

我希望pre-commit在提交代码之前先运行测试。命令python -m unittest discover在命令行中运行。

D:\project_dir>python -m unittest discover
...
...
...
----------------------------------------------------------------------
Ran 5 tests in 6.743s

OK

但是当尝试提交时,我得到了

D:\project_dir>git commit -m "fix tests with hook"
run tests................................................................Failed
hookid: tests

usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
                                       [-b] [-k TESTNAMEPATTERNS] [-s START]
                                       [-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: bigpipe_response/processors_manager.py
usage: python.exe -m unittest discover [-h] [-v] [-q] [--locals] [-f] [-c]
                                       [-b] [-k TESTNAMEPATTERNS] [-s START]
                                       [-p PATTERN] [-t TOP]
python.exe -m unittest discover: error: unrecognized arguments: tests/test_processors.py

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

-   repo: local
    hooks:
    -   id: tests
        name: run tests
        entry: python -m unittest discover
        language: python
        types: [python]
        stages: [commit]

[对于语言,我也尝试使用system。我得到了相同的结果。

我该如何解决?请帮助。

python git python-unittest pre-commit-hook
1个回答
1
投票

您可以尝试以下YAML。当然,如果使用其他模式,则应在args选项中更改模式。

-   id: unittest
    name: unittest
    entry: python -m unittest discover 
    language: python
    'types': [python]
    args: ["-p '*test.py'"] # Probably this option is absolutely not needed.
    pass_filenames: false
    stages: [commit]

您应该将false参数设置为pass_filenames,因为在其他情况下,文件将作为参数传递,并且正如您在问题中提到的,这些都是“无法识别的”参数。

© www.soinside.com 2019 - 2024. All rights reserved.