'Python pre-commit unittest faild
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.
Solution 1:[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.
Solution 2:[2]
The accepted answer won't work if your application depends on some packages. In this case, the language:python setting lets pre-commit use a custom virtual environment (within the ~/.cache folder), that you can add dependencies using the additional-dependencies flag. However this usually means copying the contents of 'requirements.txt`
So in order to use your application's virtual environment, you have to set the language:system setting and put your tests into the repo: local. Then the virtual environment that is activated when running the pre-commit hooks is used.
A working configuration would look like this:
- repo: local
hooks:
- id: unittests
name: run unit tests
entry: python -m unittest
language: system
pass_filenames: false
args: ["discover"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Flo Win |
