Search code examples
pythongitpytestpre-commit-hookpre-commit.com

Excluding pytest files/marks from pre-commit hooks


Is there a way to exclude pytests marked with pytest.mark from running during the pre-commit hook? Especially, I'd like to exclude the tests that are marked as integration tests.

The content of a tests looks like this

pytestmark = [pytest.mark.integration, pytest.mark.reporting_api]

### some tests

and the .pre-commit-conifg.yaml pytest configuration is

-   repo: local
    hooks:
    -   id: pytest
        name: pytest
        entry: pytest test/
        language: system
        pass_filenames: false
        types: [python]
        stages: [commit, push]

Solution

  • 2c: it's not a good idea to run tests as part of pre-commit -- in my experience they're going to be too slow and your contributors may get frustrated and turn off the framework entirely

    that said, it should be as simple as adding the arguments you want to either entry or args -- personally I prefer entry when working with repo: local hooks (since there's nothing that would "conventionally" override args)

    in your case this would look like:

    -   repo: local
        hooks:
        -   id: pytest
            name: pytest
            entry: pytest test/ -m 'not integration and not reporting_api'
            language: system
            pass_filenames: false
            types: [python]
            stages: [commit, push]
    

    disclaimer: I created pre-commit and I'm a pytest core dev