-
So I've been using pytest to test my applications and everything is working fine. One thing that is not really working as expected are the pre-commit hooks I've setup. repos:
# ... Other non local commits
- repo: local
hooks:
- id: pytest
name: pytest
stages: [commit]
language: system
entry: pytest
types: [python]
- id: pytest-cov
name: pytest
stages: [push]
language: system
entry: pytest --cov --cov-fail-under=70
types: [python]
pass_filenames: false While running directly from my virtualenv all tests run fine:
if I run the
As you can see, while the tests are passing, it gives this error that is related with that entry saying that could not collect tests, which I find weird since the usual run has not that problem. My requirements-dev.txt: -i https://pypi.org/simple
appdirs==1.4.4
aspy.refactor-imports==2.1.1
astpretty==2.0.0
attrs==20.2.0
bandit==1.6.2
black==20.8b1
bump2version==1.0.0
cached-property==1.5.2
cfgv==3.2.0
click==7.1.2
coverage==5.3
distlib==0.3.1
filelock==3.0.12
flake8==3.8.3
flake8-bandit==2.1.2
flake8-black==0.2.1
flake8-bugbear==20.1.4
flake8-expression-complexity==0.0.8
flake8-import-order==0.18.1
flake8-plugin-utils==1.3.1
flake8-polyfill==1.0.2
flake8-pytest-style==1.3.0
gitdb==4.0.5
gitpython==3.1.8
identify==1.5.5
iniconfig==1.0.1
mccabe==0.6.1
more-itertools==8.5.0
mypy==0.782
mypy-extensions==0.4.3
nodeenv==1.5.0
packaging==20.4
pathspec==0.8.0
pbr==5.5.0
pluggy==0.13.1
pre-commit==2.7.1
py==1.9.0
pycodestyle==2.6.0
pyflakes==2.2.0
pyparsing==3.0.0a2
pytest==6.0.2
pytest-cov==2.10.1
python-dotenv==0.14.0
pyyaml==5.3.1
regex==2020.7.14
reorder-python-imports==2.3.5
six==1.15.0
smmap==3.0.4
stevedore==3.2.2
toml==0.10.1
typed-ast==1.4.1
typing-extensions==3.7.4.3
virtualenv==20.0.31 and the pytest section from my [coverage:run]
omit = tests/*,main.py
[coverage:report]
show_missing = True
[tool:pytest]
python_files = tests/*.py
markers =
fast: marks a test as a fast execution test.
slow: marks a test as a slow execution test.
db_dependent: marks a test with database dependencies.
network_dependent: marks a test as network dependent.
integration: marks a test that is meant to be used as integration testing I'm not really sure if this is expected, but since the results are different, I believe there's something wrong either with the hook I've setup or with how pytest and pre-commit are talking to each other. |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Pytest is executed for multi sets of files, some don't provide tests and fail that execution as expected This needs a reconfigure of the pre- |
Beta Was this translation helpful? Give feedback.
-
pre-commit passes filenames as positional arguments and will invoke multiple times in parallel you're likely confused by one of these behaviours and/or not replicating that same behaviour outside pre-commit (the "no tests discovered" is probably you probably want: entry: pytest tests
pass_filenames: false
always_run: true See also this stackoverflow post but also you probably don't want to run pytest as a pre-commit hook |
Beta Was this translation helpful? Give feedback.
-
This idea seems to work. I'm sorry I didn't understand how to make this properly. I was going pre-commit route because this application has no tox config on it, and will run only on py 3.8 for now, so I thought it was not necessary. Maybe I should change my mind. Also, it is not a library, so probably I should rely on CI tools for that. It seems I made things more difficult to myself. Thanks again for the feedback and instruction. |
Beta Was this translation helpful? Give feedback.
pre-commit passes filenames as positional arguments and will invoke multiple times in parallel
you're likely confused by one of these behaviours and/or not replicating that same behaviour outside pre-commit (the "no tests discovered" is probably
pytest not/a/test/file.py another/not/a/test/file.py
for example)you probably want:
See also this stackoverflow post
but also you probably don't want to run pytest as a pre-commit hook