Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pytest passes, but pre-commit run of pytest hook fails with "0 collected". #1130

Closed
zirpu opened this issue Sep 6, 2019 · 2 comments
Closed
Labels

Comments

@zirpu
Copy link

zirpu commented Sep 6, 2019

example repo: https://github.com/zirpu/decimal_time

I can run pytest fine, but trying to commit gives me:

test/decimal_time $ git commit -mtest. decimal_time
isort....................................................................Passed
black....................................................................Passed
flake8...................................................................Passed
mypy.....................................................................Passed
pytest...................................................................Failed
hookid: pytest

============================= test session starts ==============================
platform linux -- Python 3.7.3, pytest-5.1.2, py-1.8.0, pluggy-0.12.0
rootdir: /home/allan/projects/zirpu/pykatas/test/decimal_time, inifile: pytest.ini
plugins: cov-2.7.1
collected 0 items

============================ no tests ran in 0.01s =============================

Pytest runs fine with:

======================================================= test session starts =======================================================
platform linux -- Python 3.7.3, pytest-5.0.1, py-1.8.0, pluggy-0.12.0
rootdir: /home/allan/projects/zirpu/pykatas/test/decimal_time, inifile: pytest.ini
collected 4 items                                                                                                                 

test/test_decimal_time.py ....                                                                                              [100%]

==================================================== 4 passed in 0.02 seconds =====================================================

I cannot find a way to get more info from pre-commit and pytest, pointers appreciated.

@asottile
Copy link
Member

asottile commented Sep 6, 2019

pre-commit by default passes the changed files as arguments. If you're changing non-test files, then pytest will be invoked as pytest non-test-file.py and will discover zero tests and exit with code 5 (nonzero).

Personally I wouldn't suggest running tests as part of a commit stage as they're often ~slightly too slow. That said, if you want to make this work, you can use pass_filenames: false / always_run: true -- here's an example: https://github.com/pre-commit/pygrep-hooks/blob/eb0a92ce09636b6d72787952ce0f74c5dad996fa/.pre-commit-config.yaml#L47-L53

@zirpu
Copy link
Author

zirpu commented Sep 6, 2019

ah. thanks. that works.

@zirpu zirpu closed this as completed Sep 6, 2019
garfieldnate added a commit to garfieldnate/uniunihan-db that referenced this issue Oct 16, 2020
Pre-commit passes the list of changed files to pytest; if these are
non-test files, then no tests are found, and the command fails.
Configure pre-commit to always run pytest with no arguments.

See also:
pre-commit/pre-commit#1130
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants