Python pre-commit unittest faild

可紊 提交于 2020-01-14 02:39:19

问题


I wish pre-commit to run the tests before committing my code.
The command python -m unittest discover is working in the command line.

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

OK

But when trying to commit I am getting

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

Here is my .pre-commit-config.yaml file.

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

Also for language I try to use system. I got the same result.

How can I solve this? Please help.


回答1:


You can try the following YAML. Of course, you should change the pattern in args option if you are using different one.

-   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]

You should set to false the pass_filenames parameter because in other case the files will be passed as arguments and as you mentioned in your question these are "unrecognized" parameters.



来源:https://stackoverflow.com/questions/59358182/python-pre-commit-unittest-faild

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!