show your output, we can only guess at the problem otherwise

that said, you probably want to use always_run: true and pass_filenames: false -- also your entry is bogus, no need to wrap things in bash, just call the executable directly. Putting all that together:

  - repo: local
    hooks:
      - id: pytest-check
        name: pytest-check
        entry: pytest
        language: system
        pass_filenames: false
        always_run: true

disclaimer: I am the author of pre-commit

Answer from anthony sottile on Stack Overflow
🌐
GitHub
github.com › pre-commit › pre-commit-hooks › issues › 291
Add pytest hook · Issue #291 · pre-commit/pre-commit-hooks
June 3, 2018 - pre-commit / pre-commit-hooks Public · There was an error while loading. Please reload this page. Notifications · You must be signed in to change notification settings · Fork 782 · Star 6.4k · New issueCopy link · New issueCopy link · Closed · Closed · Add pytest hook#291 ·
Author   dspechnikov
🌐
pytest
docs.pytest.org › en › 4.6.x › contributing.html
Contribution getting started — pytest documentation
Repository owners can rest assured that no pytest-dev administrator will ever make releases of your repository or take ownership in any way, except in rare cases where someone becomes unresponsive after months of contact attempts. As stated, the objective is to share maintenance and avoid “plugin-abandon”. Fork the repository. Enable and install pre-commit to ensure style-guides and code checks are followed.
Discussions

python - Running pytest as a pre-commit hook - no such file or directory issue - Stack Overflow
In my Python project I use pytest as a pre-commit hook. Some tests create and delete temporary files, everything works fine when I run pytest . However, when I run git commit ... More on stackoverflow.com
🌐 stackoverflow.com
pre-commit hook fails but pytest runs fine if called directly
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. I have the following ... More on github.com
🌐 github.com
3
2
pytest doesn't locate tests
Hi, I'm having a problem running a local command. I'm using pipenv to manage the environment, and I'm using pytest to run my tests. My tests are located in /tests, and py... More on github.com
🌐 github.com
3
April 16, 2018
I built a pre-commit hook that enforces code coverage thresholds
Without beating around the bush, this is bonkers. This belongs in CI. I'm not waiting for my tests to commit. A merge/pull request signifies my code is ready and tested. Not a commit! More on reddit.com
🌐 r/Python
23
0
March 17, 2025
🌐
Medium
medium.com › @fistralpro › pytest-pre-commit-hook-b492edd0560e
Pytest pre-commit hook
January 9, 2024 - 05 -> Run the pytest in the local .venv passing the tests folder · 08 -> pre-commit passes filenames as positional arguments and will invoke multiple times in parallel
🌐
pytest
docs.pytest.org › en › stable › contributing.html
Contributing - pytest documentation
Fetch tags from upstream if necessary (if you cloned only main git fetch --tags https://github.com/pytest-dev/pytest). Enable and install pre-commit to ensure style-guides and code checks are followed.
🌐
GitHub
github.com › christophmeissner › pytest-pre-commit
GitHub - christophmeissner/pytest-pre-commit: pre-commit hook to run pytest · GitHub
# .pre-commit-config.yaml - repo: https://github.com/christophmeissner/pytest-pre-commit rev: 1.0.0 hooks: - id: pytest pass_filenames: false always_run: true args: - "--durations=0" - "--last-failed" - "--last-failed-no-failures=all" - "--new-first" - "-rfEsxXp" - "-vvv"
Starred by 5 users
Forked by 2 users
🌐
Ansys
pre-commit-hooks.docs.ansys.com › version › stable › contribute › testing.html
Testing pre-commit hooks — ansys-pre-commit-hooks
import pytest import ansys.pre_commit_hooks.new_hook as hook def test_main(): """Test the main function of the new-hook.""" # Assert main passes assert hook.main() == 0
Find elsewhere
🌐
GitHub
github.com › szebenyib › pre-commit-pytest
GitHub - szebenyib/pre-commit-pytest: Pytest runner for the pre-commit framework · GitHub
Pytest runner for the pre-commit framework. Contribute to szebenyib/pre-commit-pytest development by creating an account on GitHub.
Author   szebenyib
🌐
Jerry Codes
blog.jerrycodes.com › pre-commit-is-awesome
Pre-commit is awesome
March 14, 2022 - Perhaps you prefer to run some of the hooks during commit and the rest during push, see top-level default_stages and repo specific stages variables. For example, maybe you want to run the whole test suite or some fast part of it (e.g. smoke tests) during push. If you are battling with a large existing Python test suite and use pytest...
🌐
GitHub
github.com › pytest-dev › pytest › blob › main › .pre-commit-config.yaml
pytest/.pre-commit-config.yaml at main · pytest-dev/pytest
exclude: docs|src/_pytest/deprecated.py|testing/deprecated_test.py|src/_pytest/legacypath.py
Author   pytest-dev
🌐
GitHub
github.com › pre-commit › pre-commit › issues › 737
pytest doesn't locate tests · Issue #737 · pre-commit/pre-commit
April 16, 2018 - pytest...................................................................Failed hookid: pytest ============================= test session starts ============================== platform darwin -- Python 3.6.5, pytest-3.5.0, py-1.5.3, pluggy-0.6.0 rootdir: <path to project>, inifile: plugins: hypothesis-3.55.6 ========================= no tests ran in 0.00 seconds ========================= ERROR: not found: <path to project>/.pre-commit-config.yaml (no name '<path to project>/.pre-commit-config.yaml' in any of []) ERROR: not found: <path to project>/Pipfile (no name '<path to project>/Pipfile' in any of []) ERROR: not found: <path to project>/Pipfile.lock (no name '<path to project>/Pipfile.lock' in any of [])
Author   zmitchell
🌐
Switowski
switowski.com › blog › pre-commit-vs-ci
pre-commit vs. CI - Sebastian Witowski
November 28, 2023 - First, run pre-commit install to install the pre-commit and post-commit hooks. Thanks to the default_install_hook_types setting, we don't have to explicitly specify which hooks we want to install (by default, the pre-commit tool only installs pre-commit hooks). Then, create a new virtual environment and install pytest there:
🌐
Pdc-support
pdc-support.github.io › software-engineering-intro › 11-pre-commit-hook
Testing using a pre-commit hook
Here we will use our previous example.py and add a git client-side hook the pre-commit hook to run a script before a commit is recorded. We are still in the directory pytest-example which contains example.py:
🌐
Medium
medium.com › @pijpijani › smarter-commits-with-git-pre-commit-hooks-9c7bb3f699bf
Smarter Commits with Git Pre-Commit Hooks | by Pikho | Medium
March 2, 2023 - Here, a test fails in pre-commit, meaning that the code we committed failed one or more tests and the pre-commit hook prevented the code from being committed to the repository. ... Here you see the same code after our first commit.
🌐
Reddit
reddit.com › r/python › i built a pre-commit hook that enforces code coverage thresholds
r/Python on Reddit: I built a pre-commit hook that enforces code coverage thresholds
March 17, 2025 -

What My Project Does

coverage-pre-commit is a Python pre-commit hook that automatically runs your tests with coverage analysis and fails commits that don't meet your specified threshold. It prevents code with insufficient test coverage from even making it to your repository, letting you catch coverage issues earlier than CI pipelines.

The hook integrates directly with the popular pre-commit framework and provides a simple command-line interface with customizable options.

Target Audience

This tool is designed for Python developers who:

  • Take test coverage seriously in production code

  • Use pre-commit hooks in their workflow

  • Want to enforce consistent coverage standards across their team

  • Need flexibility with different testing frameworks

It's production-ready and stable, with a focus on reliability and ease of integration into existing projects.

Comparison with Alternatives

  • https://github.com/Weird-Sheep-Labs/coverage-pre-commit: doesn't fail commits that don't pass a coverage threshold, what it does should probably be part of a CI pipeline.

Unlike custom scripts that you might write yourself, coverage-pre-commit:

  • Works immediately without boilerplate

  • Handles dependency management automatically

  • Supports multiple test providers with a unified interface

  • Is maintained and updated regularly

Key Features:

  • Works with unittest and pytest out of the box (with plans to add more frameworks)

  • Configurable threshold - set your own standards (default: 80%)

  • Automatic dependency management - installs what it needs

  • Customizable test commands - use your own if needed

  • Super easy setup - just add it to your pre-commit config

How to set it up:

Add this to your .pre-commit-config.yaml:

-   repo: https://github.com/gtkacz/coverage-pre-commit
    rev: v0.1.1  # Latest version
    hooks:
    -   id: coverage-pre-commit
        args: [--fail-under=95]  # If you want to set your own threshold

More examples:

Using pytest:

-   repo: https://github.com/gtkacz/coverage-pre-commit
    rev: v0.1.1
    hooks:
    -   id: coverage-pre-commit
        args: [--provider=pytest, --extra-dependencies=pytest-xdist]

Custom command:

-   repo: https://github.com/gtkacz/coverage-pre-commit
    rev: v0.1.1
    hooks:
    -   id: coverage-pre-commit
        args: [--command="coverage run --branch manage.py test"]

Any feedback, bug reports, or feature requests are always welcome! You can find the project on GitHub.

What do you all think? Any features you'd like to see added?

🌐
Substack
laszlo.substack.com › p › cq4ds-python-project-from-scratch
CQ4DS - Python project from scratch with poetry, black, ruff, pytest, pre-commit-hooks and GitHub Actions in 15 minute tops
August 3, 2023 - poetry new -n --src <project> cd <project> poetry config virtualenvs.in-project true poetry env use python3.10 source .venv/bin/activate poetry add black ruff pytest pre-commit
🌐
Vantage-ai
vantage-ai.com › blog › speed-up-your-python-development-workflow-with-pre-commit-and-makefile
Speed up your Python development workflow with pre- ...
May 1, 2021 - We were able to show a 10% improvement compared to current methods and integrate the machine learning predictions int…