pytest-probability 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pytest_probability-0.1.0/.gitignore +13 -0
- pytest_probability-0.1.0/.readthedocs.yaml +16 -0
- pytest_probability-0.1.0/LICENSE +21 -0
- pytest_probability-0.1.0/PKG-INFO +228 -0
- pytest_probability-0.1.0/README.md +204 -0
- pytest_probability-0.1.0/docs/cases.md +182 -0
- pytest_probability-0.1.0/docs/changelog.md +40 -0
- pytest_probability-0.1.0/docs/conf.py +32 -0
- pytest_probability-0.1.0/docs/contributing.md +93 -0
- pytest_probability-0.1.0/docs/cost.md +101 -0
- pytest_probability-0.1.0/docs/index.md +129 -0
- pytest_probability-0.1.0/docs/installation.md +58 -0
- pytest_probability-0.1.0/docs/internals.md +200 -0
- pytest_probability-0.1.0/docs/json-report.md +157 -0
- pytest_probability-0.1.0/docs/parametrization.md +99 -0
- pytest_probability-0.1.0/docs/quickstart.md +109 -0
- pytest_probability-0.1.0/docs/reference.md +178 -0
- pytest_probability-0.1.0/docs/requirements.txt +3 -0
- pytest_probability-0.1.0/docs/running.md +132 -0
- pytest_probability-0.1.0/pyproject.toml +39 -0
- pytest_probability-0.1.0/src/pytest_probability/__init__.py +6 -0
- pytest_probability-0.1.0/src/pytest_probability/plugin.py +704 -0
- pytest_probability-0.1.0/tests/conftest.py +1 -0
- pytest_probability-0.1.0/tests/test_plugin.py +537 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Hao Xu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: pytest-probability
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: pytest plugin for nondeterministic tests: run cases N times, report empirical pass fractions, flaky detection, and cost.
|
|
5
|
+
Project-URL: Homepage, https://github.com/xu-hao/pytest-probability
|
|
6
|
+
Project-URL: Repository, https://github.com/xu-hao/pytest-probability
|
|
7
|
+
Project-URL: Documentation, https://pytest-probability.readthedocs.io
|
|
8
|
+
Project-URL: Changelog, https://github.com/xu-hao/pytest-probability/blob/main/docs/changelog.md
|
|
9
|
+
Author: Hao Xu
|
|
10
|
+
License-Expression: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: benchmark,eval,evaluation,flaky,llm,probability,pytest
|
|
13
|
+
Classifier: Development Status :: 4 - Beta
|
|
14
|
+
Classifier: Framework :: Pytest
|
|
15
|
+
Classifier: Intended Audience :: Developers
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Topic :: Software Development :: Testing
|
|
21
|
+
Requires-Python: >=3.11
|
|
22
|
+
Requires-Dist: pytest>=7.4
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
|
|
25
|
+
# pytest-probability
|
|
26
|
+
|
|
27
|
+
[](https://pytest-probability.readthedocs.io/en/latest/)
|
|
28
|
+
[](https://pypi.org/project/pytest-probability/)
|
|
29
|
+
[](LICENSE)
|
|
30
|
+
|
|
31
|
+
**A test that passes 7 times out of 10 is not a passing test — it's a probability.**
|
|
32
|
+
|
|
33
|
+
A pytest plugin for nondeterministic code — LLM-driven functions, model
|
|
34
|
+
inference, integration points, anything flaky-prone. A single-shot test lies
|
|
35
|
+
to you: it samples a distribution once and calls the result truth.
|
|
36
|
+
pytest-probability runs every case N times and reports the empirical pass
|
|
37
|
+
fraction per case, so `7/10 FLAKY` stops hiding inside a green checkmark.
|
|
38
|
+
|
|
39
|
+
```
|
|
40
|
+
$ pytest benchmarks/ --prob-runs=10
|
|
41
|
+
...
|
|
42
|
+
================================= probability ==================================
|
|
43
|
+
[classify::is_question] classify 10/10 $0.0020
|
|
44
|
+
[classify::identify_pii] classify 7/10 $0.0020 FLAKY
|
|
45
|
+
[classify::extract_amount] classify 0/10 $0.0020 FAIL
|
|
46
|
+
[triage::refund-terse] triage 8/10 $0.0010 FLAKY
|
|
47
|
+
[triage::refund-chain_of_thought] triage 10/10 $0.0040
|
|
48
|
+
|
|
49
|
+
Overall: 35/50 passed (70%)
|
|
50
|
+
Cost: $0.0110
|
|
51
|
+
Tokens: m-small 1,200 in / 80 out / 640 cached $0.0010
|
|
52
|
+
m-large 4,800 in / 900 out $0.0040
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Documentation
|
|
56
|
+
|
|
57
|
+
Full documentation at
|
|
58
|
+
**[pytest-probability.readthedocs.io](https://pytest-probability.readthedocs.io)** —
|
|
59
|
+
installation, a quickstart, guides for benchmark files, parametrization,
|
|
60
|
+
running, cost accounting and the JSON report, a complete options
|
|
61
|
+
reference, and a developer guide covering the plugin's internals.
|
|
62
|
+
|
|
63
|
+
To build locally: `pip install -r docs/requirements.txt && sphinx-build -W -b html docs docs/_build/html`.
|
|
64
|
+
|
|
65
|
+
## Install
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
pip install pytest-probability
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
No configuration needed — the plugin registers itself via the `pytest11`
|
|
72
|
+
entry point. The only dependency is pytest itself. Requires Python 3.11+
|
|
73
|
+
and pytest 7.4+.
|
|
74
|
+
|
|
75
|
+
## Usage
|
|
76
|
+
|
|
77
|
+
Benchmark files are named `bench_*.py`, and every `bench_*` function in
|
|
78
|
+
them is a benchmark — the same convention pytest applies to `test_*`.
|
|
79
|
+
There is no plugin syntax to learn: cases are stock
|
|
80
|
+
`@pytest.mark.parametrize`, values arrive as function arguments, and
|
|
81
|
+
the function yields one result per step instead of asserting:
|
|
82
|
+
|
|
83
|
+
```python
|
|
84
|
+
import time
|
|
85
|
+
import pytest
|
|
86
|
+
from pytest_probability import StepResult
|
|
87
|
+
|
|
88
|
+
@pytest.mark.parametrize("text,expected", [
|
|
89
|
+
pytest.param("is this a question?", "question", id="is_question"),
|
|
90
|
+
pytest.param("my ssn is 078-05-1120", "pii", id="identify_pii",
|
|
91
|
+
marks=pytest.mark.fast),
|
|
92
|
+
])
|
|
93
|
+
def bench_classify(text, expected):
|
|
94
|
+
t0 = time.time()
|
|
95
|
+
answer = my_classifier(text)
|
|
96
|
+
yield StepResult(
|
|
97
|
+
label="classify",
|
|
98
|
+
passed=(answer == expected),
|
|
99
|
+
elapsed=time.time() - t0,
|
|
100
|
+
cost=0.0002, # optional — summed into the report
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Every parameter combination is a case with its own fraction row,
|
|
105
|
+
namespaced by function (`classify::identify_pii`); items collect as
|
|
106
|
+
`bench_x.py::bench_classify::identify_pii[run3]`. `pytest.param`
|
|
107
|
+
names and marks individual cases; a function with no parametrize runs
|
|
108
|
+
as a single case. Dataset-driven suites are a comprehension away:
|
|
109
|
+
`[pytest.param(row, id=row["id"]) for row in load_dataset()]`.
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
pytest benchmarks/ --prob-runs=10 # every case ×10
|
|
113
|
+
pytest benchmarks/ -k identify_pii # one case, by id
|
|
114
|
+
pytest benchmarks/ -m "fast and not eu" # marks, boolean expressions
|
|
115
|
+
pytest benchmarks/ -n 4 # parallel via pytest-xdist
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
Module-level `setup()` / `teardown()` run once per benchmark file.
|
|
119
|
+
|
|
120
|
+
### Comparison axes
|
|
121
|
+
|
|
122
|
+
Stacked decorators cross-product with pytest's exact id layout and
|
|
123
|
+
ordering — put the configurations you're comparing (models, prompts,
|
|
124
|
+
thresholds) on their own axes and each combination gets its own row:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
import pytest
|
|
128
|
+
from pytest_probability import StepResult, TokenUsage
|
|
129
|
+
|
|
130
|
+
@pytest.mark.parametrize("style", ["terse", "chain_of_thought"])
|
|
131
|
+
@pytest.mark.parametrize("text", [
|
|
132
|
+
pytest.param("my card was charged twice", id="refund"),
|
|
133
|
+
])
|
|
134
|
+
def bench_triage(text, style):
|
|
135
|
+
response = my_classifier(text, prompt_style=style)
|
|
136
|
+
yield StepResult(
|
|
137
|
+
label="triage",
|
|
138
|
+
passed=response.answer == "billing",
|
|
139
|
+
usage=[TokenUsage(model=response.model,
|
|
140
|
+
input_tokens=response.input_tokens,
|
|
141
|
+
output_tokens=response.output_tokens,
|
|
142
|
+
cost=response.cost)],
|
|
143
|
+
)
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
```
|
|
147
|
+
[triage::refund-terse] triage 8/10 $0.0010 FLAKY
|
|
148
|
+
[triage::refund-chain_of_thought] triage 10/10 $0.0040
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Options
|
|
152
|
+
|
|
153
|
+
| Option | Where | Default | Meaning |
|
|
154
|
+
|---|---|---|---|
|
|
155
|
+
| `--prob-runs=N` | CLI | ini or 1 | run every case N times |
|
|
156
|
+
| `--prob-json=PATH` | CLI | — | write a JSON report to PATH |
|
|
157
|
+
| `--prob-delay=SECONDS` | CLI | ini or 0 | sleep between case executions |
|
|
158
|
+
| `--prob-transpose` | CLI | ini or off | run-major order: run 1 of everything, then run 2, … |
|
|
159
|
+
| `prob_delay` | ini | 0 | default for `--prob-delay` |
|
|
160
|
+
| `prob_transpose` | ini | false | default for `--prob-transpose` |
|
|
161
|
+
| `prob_runs` | ini | 1 | default for `--prob-runs` |
|
|
162
|
+
| `prob_pattern` | ini | `bench_*.py` | glob for benchmark files |
|
|
163
|
+
|
|
164
|
+
## JSON report
|
|
165
|
+
|
|
166
|
+
`--prob-json=report.json` writes a single machine-readable file (think
|
|
167
|
+
`--junitxml`, but for fractions) — aggregate rows plus the raw per-run
|
|
168
|
+
step records for downstream analysis:
|
|
169
|
+
|
|
170
|
+
```json
|
|
171
|
+
{
|
|
172
|
+
"created": "2026-07-07T21:43:18",
|
|
173
|
+
"runs": 10,
|
|
174
|
+
"exit_status": 1,
|
|
175
|
+
"totals": {"passes": 35, "fails": 15, "errors": 0, "count": 50,
|
|
176
|
+
"pass_rate": 70.0, "cost": 0.011,
|
|
177
|
+
"usage": {"m-small": {"input_tokens": 1200, "output_tokens": 80,
|
|
178
|
+
"cached_input_tokens": 640, "cost": 0.001}}},
|
|
179
|
+
"rows": [
|
|
180
|
+
{"case": "classify::identify_pii", "label": "classify", "passes": 7,
|
|
181
|
+
"fails": 3, "errors": 0, "total": 10, "pass_rate": 70.0,
|
|
182
|
+
"status": "flaky", "cost": 0.002, "usage": {}}
|
|
183
|
+
],
|
|
184
|
+
"records": [
|
|
185
|
+
{"case": "classify::identify_pii", "run": 3, "steps": [
|
|
186
|
+
{"label": "classify", "passed": false, "elapsed": 0.01, "error": null,
|
|
187
|
+
"message": "got 'other'", "cost": 0.0002, "usage": []}]}
|
|
188
|
+
]
|
|
189
|
+
}
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
Under pytest-xdist the controller writes the file with the full result
|
|
193
|
+
set; workers never write partial reports.
|
|
194
|
+
|
|
195
|
+
## Semantics
|
|
196
|
+
|
|
197
|
+
- One pytest item per **(function, case, run)** —
|
|
198
|
+
`bench_x.py::bench_classify::identify_pii[run3]`. `-k`, `-x`, `--lf`,
|
|
199
|
+
JUnit XML, and xdist all operate per run.
|
|
200
|
+
- A failing step fails that run's item with a compact message (no traceback
|
|
201
|
+
noise); an exception in a bench function is a normal pytest failure *and*
|
|
202
|
+
counts as an `error` step in the aggregate.
|
|
203
|
+
- Runs are a three-class outcome — pass, fail, error — and the row status
|
|
204
|
+
names the combination: `FLAKY` is reserved for genuine pass/fail
|
|
205
|
+
nondeterminism, while passes mixed with errors show `7/10 3 ERRORED`
|
|
206
|
+
(the model never answered wrong; the harness dropped runs). Fractions
|
|
207
|
+
are always over all runs.
|
|
208
|
+
- Selection is pure pytest: `-k` over composed case ids, `-m` over marks
|
|
209
|
+
(with and/or/not), node ids for single runs. Note pytest's `-k` grammar
|
|
210
|
+
rejects `=` — select on id substrings and mark names.
|
|
211
|
+
- Steps are consumed by duck-typing: anything with `label`, `passed`, and
|
|
212
|
+
optionally `elapsed` / `error` / `message` / `cost` / `usage` works,
|
|
213
|
+
including types from other frameworks. `usage` entries (objects or plain
|
|
214
|
+
dicts with `model` and token fields) aggregate per model into the
|
|
215
|
+
`Tokens:` block and the JSON report.
|
|
216
|
+
- The fraction summary is computed from `report.user_properties`, so it
|
|
217
|
+
stays correct under pytest-xdist (workers serialize results back to the
|
|
218
|
+
controller, which renders the table).
|
|
219
|
+
- Exit code follows pytest: any failed or errored run fails the session,
|
|
220
|
+
so a flaky case exits nonzero. For softer CI gates (e.g. alert only
|
|
221
|
+
below 80%), policy-check the JSON report in a follow-up step.
|
|
222
|
+
- `--prob-delay` throttles between executions and never sleeps before the
|
|
223
|
+
first; under pytest-xdist each worker throttles its own stream.
|
|
224
|
+
- Default order is case-major (`alpha[run1]`, `alpha[run2]`, `beta[run1]`,
|
|
225
|
+
…); `--prob-transpose` makes it run-major (`alpha[run1]`, `beta[run1]`,
|
|
226
|
+
`alpha[run2]`, …), which spreads each case's runs over time — useful
|
|
227
|
+
when back-to-back runs would hit the same transient state. Ordering is
|
|
228
|
+
advisory under xdist, which schedules items across workers itself.
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
# pytest-probability
|
|
2
|
+
|
|
3
|
+
[](https://pytest-probability.readthedocs.io/en/latest/)
|
|
4
|
+
[](https://pypi.org/project/pytest-probability/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
**A test that passes 7 times out of 10 is not a passing test — it's a probability.**
|
|
8
|
+
|
|
9
|
+
A pytest plugin for nondeterministic code — LLM-driven functions, model
|
|
10
|
+
inference, integration points, anything flaky-prone. A single-shot test lies
|
|
11
|
+
to you: it samples a distribution once and calls the result truth.
|
|
12
|
+
pytest-probability runs every case N times and reports the empirical pass
|
|
13
|
+
fraction per case, so `7/10 FLAKY` stops hiding inside a green checkmark.
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
$ pytest benchmarks/ --prob-runs=10
|
|
17
|
+
...
|
|
18
|
+
================================= probability ==================================
|
|
19
|
+
[classify::is_question] classify 10/10 $0.0020
|
|
20
|
+
[classify::identify_pii] classify 7/10 $0.0020 FLAKY
|
|
21
|
+
[classify::extract_amount] classify 0/10 $0.0020 FAIL
|
|
22
|
+
[triage::refund-terse] triage 8/10 $0.0010 FLAKY
|
|
23
|
+
[triage::refund-chain_of_thought] triage 10/10 $0.0040
|
|
24
|
+
|
|
25
|
+
Overall: 35/50 passed (70%)
|
|
26
|
+
Cost: $0.0110
|
|
27
|
+
Tokens: m-small 1,200 in / 80 out / 640 cached $0.0010
|
|
28
|
+
m-large 4,800 in / 900 out $0.0040
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
## Documentation
|
|
32
|
+
|
|
33
|
+
Full documentation at
|
|
34
|
+
**[pytest-probability.readthedocs.io](https://pytest-probability.readthedocs.io)** —
|
|
35
|
+
installation, a quickstart, guides for benchmark files, parametrization,
|
|
36
|
+
running, cost accounting and the JSON report, a complete options
|
|
37
|
+
reference, and a developer guide covering the plugin's internals.
|
|
38
|
+
|
|
39
|
+
To build locally: `pip install -r docs/requirements.txt && sphinx-build -W -b html docs docs/_build/html`.
|
|
40
|
+
|
|
41
|
+
## Install
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
pip install pytest-probability
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
No configuration needed — the plugin registers itself via the `pytest11`
|
|
48
|
+
entry point. The only dependency is pytest itself. Requires Python 3.11+
|
|
49
|
+
and pytest 7.4+.
|
|
50
|
+
|
|
51
|
+
## Usage
|
|
52
|
+
|
|
53
|
+
Benchmark files are named `bench_*.py`, and every `bench_*` function in
|
|
54
|
+
them is a benchmark — the same convention pytest applies to `test_*`.
|
|
55
|
+
There is no plugin syntax to learn: cases are stock
|
|
56
|
+
`@pytest.mark.parametrize`, values arrive as function arguments, and
|
|
57
|
+
the function yields one result per step instead of asserting:
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
import time
|
|
61
|
+
import pytest
|
|
62
|
+
from pytest_probability import StepResult
|
|
63
|
+
|
|
64
|
+
@pytest.mark.parametrize("text,expected", [
|
|
65
|
+
pytest.param("is this a question?", "question", id="is_question"),
|
|
66
|
+
pytest.param("my ssn is 078-05-1120", "pii", id="identify_pii",
|
|
67
|
+
marks=pytest.mark.fast),
|
|
68
|
+
])
|
|
69
|
+
def bench_classify(text, expected):
|
|
70
|
+
t0 = time.time()
|
|
71
|
+
answer = my_classifier(text)
|
|
72
|
+
yield StepResult(
|
|
73
|
+
label="classify",
|
|
74
|
+
passed=(answer == expected),
|
|
75
|
+
elapsed=time.time() - t0,
|
|
76
|
+
cost=0.0002, # optional — summed into the report
|
|
77
|
+
)
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
Every parameter combination is a case with its own fraction row,
|
|
81
|
+
namespaced by function (`classify::identify_pii`); items collect as
|
|
82
|
+
`bench_x.py::bench_classify::identify_pii[run3]`. `pytest.param`
|
|
83
|
+
names and marks individual cases; a function with no parametrize runs
|
|
84
|
+
as a single case. Dataset-driven suites are a comprehension away:
|
|
85
|
+
`[pytest.param(row, id=row["id"]) for row in load_dataset()]`.
|
|
86
|
+
|
|
87
|
+
```bash
|
|
88
|
+
pytest benchmarks/ --prob-runs=10 # every case ×10
|
|
89
|
+
pytest benchmarks/ -k identify_pii # one case, by id
|
|
90
|
+
pytest benchmarks/ -m "fast and not eu" # marks, boolean expressions
|
|
91
|
+
pytest benchmarks/ -n 4 # parallel via pytest-xdist
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
Module-level `setup()` / `teardown()` run once per benchmark file.
|
|
95
|
+
|
|
96
|
+
### Comparison axes
|
|
97
|
+
|
|
98
|
+
Stacked decorators cross-product with pytest's exact id layout and
|
|
99
|
+
ordering — put the configurations you're comparing (models, prompts,
|
|
100
|
+
thresholds) on their own axes and each combination gets its own row:
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
import pytest
|
|
104
|
+
from pytest_probability import StepResult, TokenUsage
|
|
105
|
+
|
|
106
|
+
@pytest.mark.parametrize("style", ["terse", "chain_of_thought"])
|
|
107
|
+
@pytest.mark.parametrize("text", [
|
|
108
|
+
pytest.param("my card was charged twice", id="refund"),
|
|
109
|
+
])
|
|
110
|
+
def bench_triage(text, style):
|
|
111
|
+
response = my_classifier(text, prompt_style=style)
|
|
112
|
+
yield StepResult(
|
|
113
|
+
label="triage",
|
|
114
|
+
passed=response.answer == "billing",
|
|
115
|
+
usage=[TokenUsage(model=response.model,
|
|
116
|
+
input_tokens=response.input_tokens,
|
|
117
|
+
output_tokens=response.output_tokens,
|
|
118
|
+
cost=response.cost)],
|
|
119
|
+
)
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
```
|
|
123
|
+
[triage::refund-terse] triage 8/10 $0.0010 FLAKY
|
|
124
|
+
[triage::refund-chain_of_thought] triage 10/10 $0.0040
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
## Options
|
|
128
|
+
|
|
129
|
+
| Option | Where | Default | Meaning |
|
|
130
|
+
|---|---|---|---|
|
|
131
|
+
| `--prob-runs=N` | CLI | ini or 1 | run every case N times |
|
|
132
|
+
| `--prob-json=PATH` | CLI | — | write a JSON report to PATH |
|
|
133
|
+
| `--prob-delay=SECONDS` | CLI | ini or 0 | sleep between case executions |
|
|
134
|
+
| `--prob-transpose` | CLI | ini or off | run-major order: run 1 of everything, then run 2, … |
|
|
135
|
+
| `prob_delay` | ini | 0 | default for `--prob-delay` |
|
|
136
|
+
| `prob_transpose` | ini | false | default for `--prob-transpose` |
|
|
137
|
+
| `prob_runs` | ini | 1 | default for `--prob-runs` |
|
|
138
|
+
| `prob_pattern` | ini | `bench_*.py` | glob for benchmark files |
|
|
139
|
+
|
|
140
|
+
## JSON report
|
|
141
|
+
|
|
142
|
+
`--prob-json=report.json` writes a single machine-readable file (think
|
|
143
|
+
`--junitxml`, but for fractions) — aggregate rows plus the raw per-run
|
|
144
|
+
step records for downstream analysis:
|
|
145
|
+
|
|
146
|
+
```json
|
|
147
|
+
{
|
|
148
|
+
"created": "2026-07-07T21:43:18",
|
|
149
|
+
"runs": 10,
|
|
150
|
+
"exit_status": 1,
|
|
151
|
+
"totals": {"passes": 35, "fails": 15, "errors": 0, "count": 50,
|
|
152
|
+
"pass_rate": 70.0, "cost": 0.011,
|
|
153
|
+
"usage": {"m-small": {"input_tokens": 1200, "output_tokens": 80,
|
|
154
|
+
"cached_input_tokens": 640, "cost": 0.001}}},
|
|
155
|
+
"rows": [
|
|
156
|
+
{"case": "classify::identify_pii", "label": "classify", "passes": 7,
|
|
157
|
+
"fails": 3, "errors": 0, "total": 10, "pass_rate": 70.0,
|
|
158
|
+
"status": "flaky", "cost": 0.002, "usage": {}}
|
|
159
|
+
],
|
|
160
|
+
"records": [
|
|
161
|
+
{"case": "classify::identify_pii", "run": 3, "steps": [
|
|
162
|
+
{"label": "classify", "passed": false, "elapsed": 0.01, "error": null,
|
|
163
|
+
"message": "got 'other'", "cost": 0.0002, "usage": []}]}
|
|
164
|
+
]
|
|
165
|
+
}
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
Under pytest-xdist the controller writes the file with the full result
|
|
169
|
+
set; workers never write partial reports.
|
|
170
|
+
|
|
171
|
+
## Semantics
|
|
172
|
+
|
|
173
|
+
- One pytest item per **(function, case, run)** —
|
|
174
|
+
`bench_x.py::bench_classify::identify_pii[run3]`. `-k`, `-x`, `--lf`,
|
|
175
|
+
JUnit XML, and xdist all operate per run.
|
|
176
|
+
- A failing step fails that run's item with a compact message (no traceback
|
|
177
|
+
noise); an exception in a bench function is a normal pytest failure *and*
|
|
178
|
+
counts as an `error` step in the aggregate.
|
|
179
|
+
- Runs are a three-class outcome — pass, fail, error — and the row status
|
|
180
|
+
names the combination: `FLAKY` is reserved for genuine pass/fail
|
|
181
|
+
nondeterminism, while passes mixed with errors show `7/10 3 ERRORED`
|
|
182
|
+
(the model never answered wrong; the harness dropped runs). Fractions
|
|
183
|
+
are always over all runs.
|
|
184
|
+
- Selection is pure pytest: `-k` over composed case ids, `-m` over marks
|
|
185
|
+
(with and/or/not), node ids for single runs. Note pytest's `-k` grammar
|
|
186
|
+
rejects `=` — select on id substrings and mark names.
|
|
187
|
+
- Steps are consumed by duck-typing: anything with `label`, `passed`, and
|
|
188
|
+
optionally `elapsed` / `error` / `message` / `cost` / `usage` works,
|
|
189
|
+
including types from other frameworks. `usage` entries (objects or plain
|
|
190
|
+
dicts with `model` and token fields) aggregate per model into the
|
|
191
|
+
`Tokens:` block and the JSON report.
|
|
192
|
+
- The fraction summary is computed from `report.user_properties`, so it
|
|
193
|
+
stays correct under pytest-xdist (workers serialize results back to the
|
|
194
|
+
controller, which renders the table).
|
|
195
|
+
- Exit code follows pytest: any failed or errored run fails the session,
|
|
196
|
+
so a flaky case exits nonzero. For softer CI gates (e.g. alert only
|
|
197
|
+
below 80%), policy-check the JSON report in a follow-up step.
|
|
198
|
+
- `--prob-delay` throttles between executions and never sleeps before the
|
|
199
|
+
first; under pytest-xdist each worker throttles its own stream.
|
|
200
|
+
- Default order is case-major (`alpha[run1]`, `alpha[run2]`, `beta[run1]`,
|
|
201
|
+
…); `--prob-transpose` makes it run-major (`alpha[run1]`, `beta[run1]`,
|
|
202
|
+
`alpha[run2]`, …), which spreads each case's runs over time — useful
|
|
203
|
+
when back-to-back runs would hit the same transient state. Ordering is
|
|
204
|
+
advisory under xdist, which schedules items across workers itself.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# Benchmark files
|
|
2
|
+
|
|
3
|
+
A benchmark file is an ordinary Python module whose name matches the
|
|
4
|
+
`prob_pattern` ini option (default `bench_*.py`). Inside it, **every
|
|
5
|
+
module-level function whose name starts with `bench_` is a benchmark**
|
|
6
|
+
— the same convention pytest applies to `test_*` functions in
|
|
7
|
+
`test_*.py` files. Point pytest at a directory or at the file itself.
|
|
8
|
+
|
|
9
|
+
## The module contract
|
|
10
|
+
|
|
11
|
+
`bench_*(**params)` functions
|
|
12
|
+
: Each is a generator (any iterable-returning callable works) that
|
|
13
|
+
exercises one case and yields one step result per check. Cases come
|
|
14
|
+
from plain `@pytest.mark.parametrize` on the function — every
|
|
15
|
+
parameter combination is one case, and its values arrive as
|
|
16
|
+
arguments. A function with no parametrize marks is a single case
|
|
17
|
+
named after the function. See {doc}`parametrization`.
|
|
18
|
+
|
|
19
|
+
`setup()` / `teardown()` *(optional)*
|
|
20
|
+
: Called once per file: `setup()` before the file's first item runs,
|
|
21
|
+
`teardown()` after its last — shared by all `bench_*` functions in
|
|
22
|
+
the file. If `setup()` raises, the file's items error out
|
|
23
|
+
individually with that exception.
|
|
24
|
+
|
|
25
|
+
A file matching the pattern with no `bench_*` functions is silently
|
|
26
|
+
skipped — zero items, not an error. A file that raises on import is
|
|
27
|
+
reported as a pytest collection error, with the traceback.
|
|
28
|
+
|
|
29
|
+
```{note}
|
|
30
|
+
Benchmark files are not test modules: pytest fixtures and
|
|
31
|
+
`conftest.py` fixtures do not apply (see {doc}`internals` for why). If
|
|
32
|
+
you need per-run resources, create them inside the bench function;
|
|
33
|
+
per-file resources belong in `setup()`/`teardown()`.
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Declaring cases
|
|
37
|
+
|
|
38
|
+
Cases are stock pytest parametrization — nothing plugin-specific:
|
|
39
|
+
|
|
40
|
+
```python
|
|
41
|
+
import pytest
|
|
42
|
+
from pytest_probability import StepResult
|
|
43
|
+
|
|
44
|
+
@pytest.mark.parametrize("text,expected", [
|
|
45
|
+
pytest.param("my ssn is 078-05-1120", "pii", id="identify_pii",
|
|
46
|
+
marks=pytest.mark.fast),
|
|
47
|
+
pytest.param("is this a question?", "question", id="is_question"),
|
|
48
|
+
])
|
|
49
|
+
def bench_classify(text, expected):
|
|
50
|
+
answer = my_classifier(text)
|
|
51
|
+
yield StepResult(label="classify", passed=(answer == expected))
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
- **Values are function arguments** — named, hintable, refactorable.
|
|
55
|
+
- **`pytest.param(id=...)`** names the case; without it, pytest's id
|
|
56
|
+
rules apply (value text for scalars, `argnameN` for objects,
|
|
57
|
+
`ids=` list/callable supported).
|
|
58
|
+
- **`pytest.param(marks=...)`** tags a single case for `-m` selection
|
|
59
|
+
or skips/xfails it; marks on the function apply to every case.
|
|
60
|
+
- **Stacked decorators** cross-product with pytest's exact id layout —
|
|
61
|
+
`refund-terse`, `refund-chain_of_thought`, bottom decorator leftmost.
|
|
62
|
+
- **No parametrize at all** → the function runs as one case named
|
|
63
|
+
after itself (`bench_smoke` → case `smoke`), which makes
|
|
64
|
+
`--prob-runs` a repeat-runner for plain functions.
|
|
65
|
+
|
|
66
|
+
Each case gets its own summary row, namespaced by the function's short
|
|
67
|
+
name so identical ids in different benchmarks never collide:
|
|
68
|
+
|
|
69
|
+
```text
|
|
70
|
+
[classify::identify_pii] classify 7/10 FLAKY
|
|
71
|
+
[classify::is_question] classify 10/10
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
Dataset-driven suites are a comprehension away — and a ragged payload
|
|
75
|
+
is just a dict-valued parameter:
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
@pytest.mark.parametrize("row", [
|
|
79
|
+
pytest.param(row, id=row["id"]) for row in load_dataset()
|
|
80
|
+
])
|
|
81
|
+
def bench_extract(row):
|
|
82
|
+
...
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
## Step results
|
|
86
|
+
|
|
87
|
+
Bench functions yield one result per step:
|
|
88
|
+
|
|
89
|
+
```python
|
|
90
|
+
from pytest_probability import StepResult, TokenUsage
|
|
91
|
+
|
|
92
|
+
StepResult(
|
|
93
|
+
label="classify", # required — the row key in the summary
|
|
94
|
+
passed=True, # required — did this step pass this run?
|
|
95
|
+
elapsed=0.42, # optional — seconds, recorded in the JSON report
|
|
96
|
+
error=None, # optional — infrastructure failure text
|
|
97
|
+
message="got 'other'", # optional — shown in the failure summary
|
|
98
|
+
details={}, # optional — free-form, for your own use
|
|
99
|
+
cost=0.0002, # optional — summed into the report
|
|
100
|
+
usage=[TokenUsage(model="m-small", input_tokens=100, output_tokens=20)],
|
|
101
|
+
)
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Field semantics:
|
|
105
|
+
|
|
106
|
+
| Field | Type | Effect |
|
|
107
|
+
|---|---|---|
|
|
108
|
+
| `label` | `str` | Aggregation key: fractions are computed per `(case, label)` pair |
|
|
109
|
+
| `passed` | `bool` | `False` fails this run's item and counts toward the row's `fails` |
|
|
110
|
+
| `elapsed` | `float` | Informational; preserved in `--prob-json` records |
|
|
111
|
+
| `error` | `str \| None` | Non-`None` counts the step as an **error**, not a fail |
|
|
112
|
+
| `message` | `str \| None` | Appended to the one-line failure report |
|
|
113
|
+
| `details` | `dict` | Not interpreted by the plugin |
|
|
114
|
+
| `cost` | `float \| None` | Summed per row and overall; see {doc}`cost` |
|
|
115
|
+
| `usage` | `list[TokenUsage]` | Per-model token accounting; see {doc}`cost` |
|
|
116
|
+
|
|
117
|
+
Steps are duck-typed: anything with a `label` and `passed` attribute
|
|
118
|
+
is accepted, and the remaining fields are read with `getattr(..., None)`
|
|
119
|
+
defaults — so bench functions may yield result types from other
|
|
120
|
+
frameworks as long as the attribute names line up.
|
|
121
|
+
|
|
122
|
+
## Multi-step runs
|
|
123
|
+
|
|
124
|
+
Yield as many steps as the case has checks. Each label aggregates into
|
|
125
|
+
its own summary row, and one failing step does not stop the following
|
|
126
|
+
steps from running:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
@pytest.mark.parametrize("text", CASES)
|
|
130
|
+
def bench_pipeline(text):
|
|
131
|
+
t0 = time.time()
|
|
132
|
+
answer = pipeline(text)
|
|
133
|
+
yield StepResult(label="classify", passed=answer.kind == "billing",
|
|
134
|
+
elapsed=time.time() - t0)
|
|
135
|
+
yield StepResult(label="latency", passed=(time.time() - t0) < 2.0)
|
|
136
|
+
yield StepResult(label="citation", passed=answer.citation is not None)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
```text
|
|
140
|
+
[pipeline::refund_request] classify 9/10 FLAKY
|
|
141
|
+
[pipeline::refund_request] latency 10/10
|
|
142
|
+
[pipeline::refund_request] citation 4/10 FLAKY
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
## Failure and error semantics
|
|
146
|
+
|
|
147
|
+
Two distinct things can go wrong, and they are reported differently:
|
|
148
|
+
|
|
149
|
+
**A step fails** (`passed=False`)
|
|
150
|
+
: The run's item fails with a compact one-liner — no traceback, because
|
|
151
|
+
there is no exception to trace:
|
|
152
|
+
|
|
153
|
+
```text
|
|
154
|
+
______________________ case: classify::identify_pii ______________________
|
|
155
|
+
step 'classify' failed: got 'other'
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The text after the colon is the step's `error` or, failing that, its
|
|
159
|
+
`message`.
|
|
160
|
+
|
|
161
|
+
**The bench function raises**
|
|
162
|
+
: The item fails with the ordinary pytest traceback, *and* the run is
|
|
163
|
+
recorded as a failed step labeled `error` so it still counts in the
|
|
164
|
+
aggregate:
|
|
165
|
+
|
|
166
|
+
```text
|
|
167
|
+
[check::crashy] ok 1/1
|
|
168
|
+
[check::crashy] error 0/1 ERROR
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Steps yielded before the exception are kept.
|
|
172
|
+
|
|
173
|
+
Runs are a three-class outcome, and the row status names the
|
|
174
|
+
combination: all errors shows **ERROR** (harness problem), passes
|
|
175
|
+
mixed with errors shows **N ERRORED** (`7/10 3 ERRORED` — the model
|
|
176
|
+
never answered wrong, the harness dropped runs), no passes with real
|
|
177
|
+
failures shows **FAIL**, and passes mixed with failures shows
|
|
178
|
+
**FLAKY** — the word reserved for genuine nondeterminism. The fraction
|
|
179
|
+
is always `passes/total runs`, so an errored run costs the row its
|
|
180
|
+
perfect score; the status word says which class of trouble it was.
|
|
181
|
+
All seven class combinations and their exact renderings are tabulated
|
|
182
|
+
in {doc}`reference`.
|