Search code examples
pythoncontinuous-integrationtoxpre-commitpre-commit.com

pre-commit vs tox | what's the difference | scope of use


Tox: https://tox.wiki/en/latest/

pre-commit: https://pre-commit.com/

I would like to understand the borders for both choices.

I know that pre-commit creates a py environment - same as tox. To me, their architecture looks a bit the same.

Some people use them in combination... what pre-commit can't do, that tox can? I saw examples where with pre-commit during CI pipeline you can run unit tests, etc.

Which one is the best to integrate within the CI build?


Solution

  • In short, pre-commit is a linter/formatter runner, tox is a generic virtual env management and test command line tool.

    While tox could run linters too, it is tedious to manage the versions of the linters. In pre-commit you can just run pre-commit autoupdate, and all linters get updated.

    On the other hand tox can run e.g. a test suite or a coverage report for many different Python versions. This is not only helpful for a library, but also for an app - so you can already test the upcoming Python versions.

    tox is also used to create documentation, and sometimes also to make a release - you can't (shouldn't) do this with pre-commit.

    And tox is certainly not obsolete, although GitHub actions can test against different Python versions, as you can run tox both local and in CI.

    I gave a lightning talk on this topic: https://www.youtube.com/watch?v=OnM3KuE7MQM

    Which one is the best to integrate within the CI build?

    I like to run pre-commit via tox, both in CI and locally.

    Example tox configuration

    https://github.com/jugmac00/flask-reuploaded/blob/6496f8427a06be4a9f6a5699757ca7f9aba78ef6/tox.ini#L24-L26

    Example pre-commit configuration

    https://github.com/jugmac00/flask-reuploaded/blob/6496f8427a06be4a9f6a5699757ca7f9aba78ef6/.pre-commit-config.yaml