pytest_benchmark.fixture.BenchmarkFixture.NotEnoughTests:无法生成基准测试:未执行任何轮次(目标时间 0.0s,min_rounds 0)
pytest_benchmark.fixture.BenchmarkFixture.NotEnoughTests: Unable to produce a benchmark: no rounds executed (target time 0.0s, min_rounds 0)
ID: python/pytest-benchmark-no-results
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 4.0 | active | — | — | — |
根因分析
被基准测试的函数在 benchmark 可调用对象内部引发了异常,或者校准设置(--benchmark-min-rounds=0 --benchmark-max-time=0)将运行压缩为 0 次迭代。函数内部的异常会被 pytest-benchmark 静默吞掉并报告为 NotEnoughTests。
English
The benchmarked function raised an exception inside the benchmark callable, or calibration settings (--benchmark-min-rounds=0 --benchmark-max-time=0) collapsed the run to zero iterations. Exception inside the function is silently swallowed by pytest-benchmark and reported as NotEnoughTests.
解决方案
-
95% 成功率
def test_parse_works(): assert parse('1,2,3') == [1, 2, 3] def test_parse_bench(benchmark): result = benchmark(parse, '1,2,3') assert result == [1, 2, 3] -
85% 成功率
pytest --benchmark-verbose tests/test_bench.py -s
无效尝试
常见但无效的做法:
-
85% 失败
If the function raises on every call, more rounds still fail; the exception is the root cause, not the round count.
-
80% 失败
pytest-benchmark invokes the callable internally; wrapping the fixture call does not catch exceptions raised inside the benchmark loop.