Testing
Setting up testing with pytest and uv
Creating a Project with Tests
uv init myapp --package
d myappThis creates a Python package project with the following structure:
myapp/
├── pyproject.toml
├── README.md
└── src
└── myapp
└── __init__.pyAdding pytest as a Development Dependency
Add pytest to your project’s development dependencies:
uv add --dev pytestThis command:
Updates your
pyproject.tomlwith pytest as a development dependencyCreate 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
testsdirectorySpecifies 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?