|
| 1 | +# testing |
| 2 | + |
| 3 | +Setting up testing with pytest and uv |
| 4 | + |
| 5 | +## Creating a Project with Tests |
| 6 | + |
| 7 | +```shell |
| 8 | +uv init myapp --package |
| 9 | +d myapp |
| 10 | +``` |
| 11 | + |
| 12 | +This creates a Python package project with the following structure: |
| 13 | + |
| 14 | +``` |
| 15 | +myapp/ |
| 16 | +├── pyproject.toml |
| 17 | +├── README.md |
| 18 | +└── src |
| 19 | + └── myapp |
| 20 | + └── __init__.py |
| 21 | +``` |
| 22 | + |
| 23 | +## Adding pytest as a Development Dependency |
| 24 | + |
| 25 | +Add pytest to your project’s development dependencies: |
| 26 | + |
| 27 | +```shell |
| 28 | +uv add --dev pytest |
| 29 | +``` |
| 30 | + |
| 31 | +This command: |
| 32 | + |
| 33 | +- Updates your `pyproject.toml` with pytest as a development dependency |
| 34 | +- Create the project’s lockfile |
| 35 | +- Installs pytest in your project’s virtual environment |
| 36 | + |
| 37 | +## Creating a Simple Module to Test |
| 38 | + |
| 39 | +Let’s create a simple calculator module to test. |
| 40 | +Create a new file at `src/myapp/calculator.py`: |
| 41 | + |
| 42 | +```python |
| 43 | +def add(a, b): |
| 44 | + return a + b |
| 45 | + |
| 46 | + |
| 47 | +def subtract(a, b): |
| 48 | + return a - b |
| 49 | + |
| 50 | + |
| 51 | +def multiply(a, b): |
| 52 | + return a * b |
| 53 | + |
| 54 | + |
| 55 | +def divide(a, b): |
| 56 | + if b == 0: |
| 57 | + raise ValueError("Cannot divide by zero") |
| 58 | + return a / b |
| 59 | +``` |
| 60 | + |
| 61 | +## Creating Test Files |
| 62 | + |
| 63 | +Create a `tests` directory at the root of your project |
| 64 | + |
| 65 | +Now, create a test file for our calculator module in tests/test_calculator.py: |
| 66 | + |
| 67 | +```python |
| 68 | +import pytest |
| 69 | +from myapp.calculator import add, subtract, multiply, divide |
| 70 | + |
| 71 | + |
| 72 | +def test_add(): |
| 73 | + assert add(1, 2) == 3 |
| 74 | + assert add(-1, 1) == 0 |
| 75 | + assert add(-1, -1) == -2 |
| 76 | + |
| 77 | + |
| 78 | +def test_subtract(): |
| 79 | + assert subtract(3, 2) == 1 |
| 80 | + assert subtract(2, 3) == -1 |
| 81 | + assert subtract(0, 0) == 0 |
| 82 | + |
| 83 | + |
| 84 | +def test_multiply(): |
| 85 | + assert multiply(2, 3) == 6 |
| 86 | + assert multiply(-2, 3) == -6 |
| 87 | + assert multiply(-2, -3) == 6 |
| 88 | + |
| 89 | + |
| 90 | +def test_divide(): |
| 91 | + assert divide(6, 3) == 2 |
| 92 | + assert divide(6, -3) == -2 |
| 93 | + assert divide(-6, -3) == 2 |
| 94 | + |
| 95 | + |
| 96 | +def test_divide_by_zero(): |
| 97 | + with pytest.raises(ValueError): |
| 98 | + divide(5, 0) |
| 99 | +``` |
| 100 | + |
| 101 | +## Configuring pytest |
| 102 | + |
| 103 | +Let’s configure pytest in your pyproject.toml file by adding these settings: |
| 104 | + |
| 105 | +```yml |
| 106 | +[tool.pytest.ini_options] |
| 107 | +log_cli = true |
| 108 | +testpaths = ["tests"] |
| 109 | +python_files = "test_*.py" |
| 110 | +python_functions = "test_*" |
| 111 | +addopts = ["-v", "--strict-markers"] |
| 112 | +pythonpath = ["."] |
| 113 | +``` |
| 114 | + |
| 115 | +This configuration: |
| 116 | + |
| 117 | +- Sets the test discovery path to the `tests` directory |
| 118 | +- Specifies that test files should start with `test_` |
| 119 | +- Specifies that test functions should start with `test_` |
| 120 | + |
| 121 | +## Running Tests |
| 122 | + |
| 123 | +Now you can run your tests using uv: |
| 124 | + |
| 125 | +```shell |
| 126 | +uv run pytest |
| 127 | +``` |
| 128 | + |
| 129 | +To see more detailed output, use the verbose flag: |
| 130 | + |
| 131 | +```shell |
| 132 | +uv run pytest --cov=testing_demo |
| 133 | +``` |
| 134 | + |
| 135 | +For a more detailed report: |
| 136 | + |
| 137 | +```shell |
| 138 | +uv run pytest --cov=testing_demo --cov-report=term-missing |
| 139 | +``` |
| 140 | + |
| 141 | +To test a specific test case in a test file |
| 142 | + |
| 143 | +```shell |
| 144 | +uv run pytest tests/integration/agent/test_basic.py::test_basic |
| 145 | +``` |
0 commit comments