Pytest Test Framework¶
Pytest is a powerful and flexible testing framework for Python. It simplifies writing and running tests, making it an essential tool for ensuring code quality.
Installation¶
To install Pytest, use uv:
uv pip install pytest
Basic Usage¶
Create Test Files:
Tests are typically placed in a
tests/directory.Test file names should start with
test_or end with_test.py.Test functions should start with
test_.
Write Tests:
Use
assertstatements to verify expected behavior.
# tests/test_example.py def add(a, b): return a + b def test_add(): assert add(2, 3) == 5 assert add(-1, 1) == 0 assert add(0, 0) == 0
Run Tests:
Navigate to the project root directory.
Run
uvx pytestor justpytestif your virtual environment is activated.
uvx pytest
Key Features¶
Simple Assertions: Use standard
assertstatements for test conditions.Test Discovery: Automatically finds test files and functions.
Fixtures: Define reusable test components.
Parametrization: Run the same test with multiple inputs.
Plugins: Extend functionality with a wide range of plugins.
Configuration¶
Pytest can be configured using a pytest.ini, pyproject.toml, or tox.ini file. Here’s an example pyproject.toml configuration:
# pyproject.toml
[tool.pytest.ini_options]
testpaths = ["tests"]
python_files = ["test_*.py"]
testpaths: Specifies the directories to search for tests.python_files: Specifies the file naming pattern for test files.
Fixtures¶
Fixtures are functions that provide a fixed baseline for tests. They are used to set up and tear down resources, such as database connections or temporary files.
import pytest
@pytest.fixture
def temp_file(tmp_path):
file = tmp_path / "tempfile.txt"
file.write_text("Hello, Pytest!")
return file
def test_file_content(temp_file):
assert temp_file.read_text() == "Hello, Pytest!"
Best Practices¶
Keep Tests Isolated: Each test should be independent and not rely on the state of other tests.
Use Descriptive Names: Test function names should clearly describe what is being tested.
Test Edge Cases: Include tests for boundary conditions and error handling.
Use Fixtures Wisely: Use fixtures to reduce code duplication and improve test readability.
Follow the Arrange-Act-Assert Pattern:
Arrange: Set up the test environment and prepare the inputs.
Act: Execute the code being tested.
Assert: Verify the expected results.
Troubleshooting¶
Tests Not Being Discovered:
Ensure test files and functions follow the naming conventions.
Check the
testpathsandpython_filessettings inpyproject.toml.
Import Errors:
Make sure your project is installed in editable mode (
uv pip install --editable .).Verify that your virtual environment is activated.
By following these guidelines, you can effectively use Pytest to write and run tests, ensuring the reliability and quality of your Python code.