python
runtime_error
ai_generated
true
夹具 'data' 被直接调用。夹具不是用来直接调用的,而是由 pytest 自动提供的。
Fixture 'data' called directly. Fixtures are not meant to be called directly, but are provided automatically by pytest.
ID: python/pytest-fixture-return-none
80%修复率
86%置信度
0证据数
2025-04-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.10 | active | — | — | — |
根因分析
测试或夹具尝试直接调用夹具函数(如 data()),而不是将其作为参数使用,导致 pytest 引发错误,因为夹具不是常规函数。
English
A test or fixture tries to call a fixture function directly (e.g., data()) instead of using it as a parameter, causing pytest to raise an error because fixtures are not regular functions.
解决方案
-
95% 成功率
Use the fixture as a parameter in the test function: def test_foo(data): instead of calling data().
-
80% 成功率
If you need to call a fixture programmatically, use the request fixture: request.getfixturevalue('data')
无效尝试
常见但无效的做法:
-
70% 失败
Adding a return value to the fixture does not fix the issue because the fixture is still being called incorrectly.
-
50% 失败
Converting the fixture to a regular function may work but loses the benefits of fixture scoping and dependency injection.