Python 预提交单元测试失败

sha*_* te 3 python git pre-commit-hook python-unittest pre-commit.com

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

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

OK
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

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

-   repo: local
    hooks:
    -   id: tests
        name: run tests
        entry: python -m unittest discover
        language: python
        types: [python]
        stages: [commit]
Run Code Online (Sandbox Code Playgroud)

同样对于我尝试使用的语言system。我得到了同样的结果。

我该如何解决这个问题?请帮忙。

mil*_*azs 6

您可以尝试以下 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]
Run Code Online (Sandbox Code Playgroud)

你应该设置falsepass_filenames参数,因为在其他情况下,文件将作为参数传递,当你在你的问题中提到这些都是“无法识别”参数。