Python预提交单元测试失败 [英] Python pre-commit unittest faild
问题描述
我希望 pre-commit
在提交代码之前先运行测试.
命令python -m unittest discover
在命令行中运行.
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
这是我的.pre-commit-config.yaml
文件.
- repo: local
hooks:
- id: tests
name: run tests
entry: python -m unittest discover
language: python
types: [python]
stages: [commit]
对于语言,我也尝试使用system
.我得到了相同的结果.
Also for language I try to use system
. I got the same result.
我该如何解决?请帮忙.
How can I solve this? Please help.
推荐答案
您可以尝试以下YAML.当然,如果您使用其他模式,则应在args
选项中更改模式.
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]
您应该将pass_filenames
参数设置为false
,因为在其他情况下,文件将作为参数传递,并且正如您在问题中提到的那样,这些参数是无法识别的"参数.
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.
这篇关于Python预提交单元测试失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!