Audrey M. Roy Greenfeld

Quietly building the future.

Adding Pytest and Tox to a Rye Python Project

2024-07-07

rye add pytest

In src/ create tests/test_main.py:

def test_main():
    assert True

Run the tests:

rye test

Then add Tox:

rye add tox

Configure Tox in tox.ini (not pyproject.toml because Tox support for it is a bit limited)):

[tox]
envlist = py311, py312

Tox will create virtual environments for each Python version you specify in envlist, install the dependencies, and run the tests in each environment.

Then to run the tests with Tox, you can run either of these commands:

tox
rye run tox

You can also run tox in CI, e.g. GitHub Actions, to ensure your tests pass in multiple Python versions.