Testing

Setting up testing with pytest and uv

Creating a Project with Tests

uv init myapp --package
d myapp

This creates a Python package project with the following structure:

myapp/
├── pyproject.toml
├── README.md
└── src
    └── myapp
        └── __init__.py

Adding pytest as a Development Dependency

Add pytest to your project’s development dependencies:

uv add --dev pytest

This command:

  • Updates your pyproject.toml with pytest as a development dependency

  • Create the project’s lockfile

  • Installs pytest in your project’s virtual environment

Creating a Simple Module to Test

Let’s create a simple calculator module to test. Create a new file at src/myapp/calculator.py:

Creating Test Files

Create a tests directory at the root of your project

Now, create a test file for our calculator module in tests/test_calculator.py:

Configuring pytest

Let’s configure pytest in your pyproject.toml file by adding these settings:

This configuration:

  • Sets the test discovery path to the tests directory

  • Specifies that test files should start with test_

  • Specifies that test functions should start with test_

Running Tests

Now you can run your tests using uv:

To see more detailed output, use the verbose flag:

For a more detailed report:

To test a specific test case in a test file

Last updated

Was this helpful?