How to run django tests in a pre-commit hook

Viewed 1190

I'm stuck on how to simply run my django tests in a pre-commit hook. I will eventually put them in the push stage, but I'm just trying to get them to run first. It's trying to loop over my tests, but the tests object is a NoneType object. Works fine outside of this hook. I'm sure this is obvious to someone. Thanks.

This is my test hook.

repo: local
    hooks:
    -   id: tests
        name: run tests
        always_run: true
        entry: python manage.py test
        language: system
        types: [python]

Edit: I have since realised it's not a good idea to run backend tests on commits or pushes. Should only be running hooks to clean up code on git hooks.

It’s not a good idea to run backend tests regularly like this because as your app gets bigger the number of tests to run will get bigger and you will slow down your development. If your tests are anything like mine you will have created a lot of objects in your test db that will also need to be destroyed. Instead you should run the tests regularly outside of your hook, and lastly have them run in your deployment pipeline.

1 Answers

I had the same issue now but it works fine with this:

 repos:
  - repo: local
    hooks:
      - id: django-test
        name: django-test
        entry: python manage.py test
        always_run: true
        pass_filenames: false
        language: system
Related