RecursionError: 调用 Python 对象时超出最大递归深度 ... conftest.py:10: in db request.getfixturevalue('db') conftest.py:10: in db request.getfixturevalue('db') ...
RecursionError: maximum recursion depth exceeded while calling a Python object ... conftest.py:10: in db request.getfixturevalue('db') conftest.py:10: in db request.getfixturevalue('db') ...
ID: python/pytest-recursion-error-fixture-cycle
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 6.x | active | — | — | — |
| 7.x | active | — | — | — |
| 8.x | active | — | — | — |
根因分析
某个 fixture 调用 request.getfixturevalue() 时传入了自身的名称,导致无限递归。这通常发生在重构 fixture 时意外引用了错误的名称。
English
A fixture calls request.getfixturevalue() with its own name, creating an infinite recursion. This often happens when refactoring fixtures and accidentally referencing the wrong name.
解决方案
-
98% 成功率
@pytest.fixture def db(request): engine = request.getfixturevalue('engine') # not 'db' return engine.connect() -
95% 成功率
@pytest.fixture def db(engine): # pytest injects engine directly return engine.connect()
无效尝试
常见但无效的做法:
-
90% 失败
Delays the crash but the infinite recursion still exhausts memory and eventually raises RecursionError or crashes the interpreter.
-
95% 失败
The recursion happens during fixture resolution before the try block can catch it in a useful way.