我正在尝试调试使用 tox 进行单元测试的 python 代码库。由于弄清楚,失败的测试之一被证明很困难,我想使用 pudb 来逐步执行代码。
一开始想到,会想到刚才pip install pudb在单元测试代码中加入import pudb和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)
Run Code Online (Sandbox Code Playgroud)
注意到 .tox 项目文件夹会让人意识到在 tox 中有一个 site-packages 文件夹,这是有道理的,因为 tox 的重点是管理不同 virtualenv 场景下的测试。这也意味着有一个 tox.ini 配置文件,其 deps 部分可能如下所示:
[tox]
envlist = lint, py3
[testenv]
deps =
pytest
commands = pytest tests
Run Code Online (Sandbox Code Playgroud)
添加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
Run Code Online (Sandbox Code Playgroud)
所以,我被困在这一点上。在 Tox 中不能使用 pudb 而不是 pdb 吗?
有一个包称为pytest-pudb覆盖自动测试环境(如 tox)中的 pudb 入口点以成功跳转到调试器。
要使用它,只需让您的 tox.ini 文件在其 testenv 依赖项中同时包含pudb和pytest-pudb条目,类似于:
[tox]
envlist = lint, py3
[testenv]
deps =
pytest
pudb
pytest-pudb
commands = pytest tests
Run Code Online (Sandbox Code Playgroud)