Home > Net >  pytest --cov shows detailed report in command line, but not during githook pre-commit
pytest --cov shows detailed report in command line, but not during githook pre-commit

Time:05-26

When I run pytest src/packages --cov --cov-branch --cov-report term-missing, I get a detailed report with Stmts | Miss | Branch | BrPart | Cover | Missing as shown in screen shot below:

Report in Command Line Working

However, when I run pre-commit run pytest-cov-src-packages (or git commit) with the pre-commit hook shown below, I just get a "pytest-cov-src-packages..................................................Passed" statement in the command line. No report or anything. Just passed.

  - id: pytest-cov-src-packages
    name: pytest-cov-src-packages
    language: system
    entry: pytest src/packages --cov --cov-branch --cov-report term-missing 
    types: [python]
    stages: [commit]
    pass_filenames: false
    always_run: true

Is there any reason why running pytest in command line vs. firing off the entry: snippet from the pre-commit yields a different result? I would love to have the detailed report in the command line as shown in the image working during the pre-commit process as part of the git commit.

Thank you!

CodePudding user response:

Is there any reason why running pytest in command line vs. firing off the entry: snippet from the pre-commit yields a different result?

Because pre-commit is often configured to run many tests, it is designed to provide summary output by default. Any output produced by your pre-commit entries is discarded for successful tests; you will only see the output in the event of a failure.

If you always want to see the output, you can run pre-commit with the -v flag.

Compare:

$ pre-commit run --all-files
pytest-cov-src-packages..................................................Passed

With:

$ pre-commit run --all-files -v
pytest-cov-src-packages..................................................Passed
- hook id: pytest-cov-src-packages
- duration: 0.21s

============================= test session starts ==============================
platform linux -- Python 3.10.4, pytest-7.1.2, pluggy-1.0.0
rootdir: /home/lars/tmp/python
plugins: cov-3.0.0
collected 1 item

test_foo.py .                                                            [100%]

---------- coverage: platform linux, python 3.10.4-final-0 -----------
Name          Stmts   Miss Branch BrPart  Cover   Missing
---------------------------------------------------------
test_foo.py       2      0      0      0   100%
---------------------------------------------------------
TOTAL             2      0      0      0   100%


============================== 1 passed in 0.04s ===============================
  • Related