Post

Run Python Tests and Formatting Inside WSL

Add pytest and Ruff to a small Python project inside WSL with a reusable pyproject.toml configuration.

Run Python Tests and Formatting Inside WSL

Once Python works inside WSL, add a basic test and formatting workflow. This keeps feedback fast before a project grows.

Create the Project

1
2
3
4
5
6
mkdir -p ~/projects/hello-python/tests
cd ~/projects/hello-python
python3 -m venv .venv
source .venv/bin/activate
pip install --upgrade pip
pip install pytest ruff

Create calculator.py:

1
2
def add(left: int, right: int) -> int:
    return left + right

Create tests/test_calculator.py:

1
2
3
4
5
from calculator import add


def test_add() -> None:
    assert add(2, 3) == 5

Add Project Configuration

Create pyproject.toml:

1
2
3
4
5
6
7
8
[tool.pytest.ini_options]
testpaths = ["tests"]

[tool.ruff]
line-length = 88

[tool.ruff.lint]
select = ["E", "F", "I"]

Run the Checks

1
2
3
pytest
ruff check .
ruff format --check .

Apply formatting with ruff format .. Use ruff check --fix . only after reviewing the lint findings because automatic fixes can change imports or code structure.

Add a Check Script

Create check.sh:

1
2
3
4
5
6
#!/usr/bin/env bash
set -eu

pytest
ruff check .
ruff format --check .

Run it with bash check.sh.

Next Steps

For a repeatable container environment, continue with Set Up VS Code Dev Containers on WSL 2.

References

This post is licensed under CC BY 4.0 by the author.