Python pre-commit unittest faild
Asked Answered
S

2

5

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.

Subdebutante answered 16/12, 2019 at 14:0 Comment(3)
What program are you using to run this pre-commit hook?Extrados
@Extrados what do you mean by what program ?Subdebutante
git doesn't natively understand your .pre-commit-config.yaml file. All git understands are scripts in the .git/hooks/ folder. It looks like the program that uses this file is the pre-commit program.Extrados
D
10

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.

Debbydebee answered 16/12, 2019 at 15:16 Comment(1)
thank you @Debbydebee this was very helpful. indeed is was the pass_filenames: falseSubdebutante
O
5

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"]
Outwardly answered 29/3, 2022 at 11:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.