How to add unittest to pre-commit

Tags:

I was looking for the predefined hooks but then learned that unittest can be easily written myself.

What’s in the below runs all tests of the project. If you don’t use poetry, remove ‘poetry run’ from ‘entry’.

# $ brew install pre-commit
# $ pre-commit install
# $ pre-commit run --all-files
repos:
    - repo: https://github.com/pre-commit/pre-commit-hooks
      rev: v4.5.0
      hooks:
          - id: check-yaml
          - id: end-of-file-fixer
            exclude: ".*/resources/.*"
          - id: trailing-whitespace
          - id: detect-private-key
    - repo: https://github.com/astral-sh/ruff-pre-commit
      rev: v0.3.7
      hooks:
          - id: ruff  # Linter
            types_or: [python, pyi, jupyter]
            args: [--fix]
          - id: ruff-format  # Formatter
            types_or: [python, pyi, jupyter]
    - repo: local
      hooks:
          - id: unittest
            name: unittest
            entry: poetry run python -m unittest discover -p '*_test.py'
            language: system
            types: [python]
            pass_filenames: false
            verbose: true
            stages: [commit]

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *