我正在尝试调试一个使用 tox 进行单元测试的 python 代码库。由于弄清楚,其中一个失败的测试证明很困难,我想使用 pudb 来逐步执行代码。
乍一看,会想到刚才pip install pudb
在单元测试代码中添加import pudb
and pudb.settrace()
。但这会导致 ModuleNotFoundError:
> import pudb
>E ModuleNotFoundError: No module named 'pudb'
>tests/mytest.py:130: ModuleNotFoundError
> ERROR: InvocationError for command '/Users/me/myproject/.tox/py3/bin/pytest tests' (exited with code 1)
注意到 .tox 项目文件夹会让人意识到在 tox 中有一个 site-packages 文件夹,这是有道理的,因为 tox 的目的是管理不同 virtualenv 场景下的测试。这也意味着有一个 tox.ini 配置文件,其中的 deps 部分可能如下所示:
[tox]
envlist = lint, py3
[testenv]
deps =
pytest
commands = pytest tests
添加pudb
到 deps 列表应该可以解决 ModuleNotFoundError,但会导致另一个错误:
self = <_pytest.capture.DontReadFromInput object at 0x103bd2b00>
def fileno(self):
> raise UnsupportedOperation("redirected stdin is pseudofile, "
"has no fileno()")
E io.UnsupportedOperation: redirected stdin is pseudofile, has no fileno()
.tox/py3/lib/python3.6/site-packages/_pytest/capture.py:583: UnsupportedOperation
所以,我被困在这一点上。不能在 Tox 中使用 pudb 代替 pdb 吗?