python - 如何使用带有选项卡完成功能的 python 调试器运行 pytest?

标签 python debugging pytest pdb ipdb

背景

我用简单的 from IPython import embed; 调试我的 python 脚本已有 2 年了; embed(),它一直工作得很好。我只是将命令放在我想检查的行上,当运行脚本时,我将拥有完整的 IPython shell,具有检查变量、定义函数等的能力。最重要的是,IPython shell 带有变量名选项卡完成。

现在,不再总是使用 from IPython import embed 定义“暂停线”; embed(),我希望我的 python 脚本在运行测试时遇到异常时暂停执行。

问题

你如何以这种方式运行 pytest

  1. 当出现异常时,它会启动 python 调试器
  2. 这个调试器有变量名的tab 补全

设置

  • Windows 10
  • python 3.6

简单失败测试

# test_somemodule.py
def test_me(some_variable):
    x = 1 + some_variable
    return x


test_me('I am a string')

(不,您不会像这样为 pytest 编写测试,但对于这个示例性目的来说,这很好。)

不起作用的解决方案

1。 pytest --pdb(没有安装 pdbpp)

这将打开基本的 pdb运行到错误时的shell。但它没有制表符补全。

-> x = 1 + some_variable
(Pdb) so[<tab_here_produces_tab>]

2。 pytest --pdb(安装了 pdbpp)

这将打开基本的 pdbpp运行到错误时的shell。但是没有制表符补全。

-> x = 1 + some_variable
(Pdb++) so[<tab_here_produces_tab>]

3。 pytest --pdb --pdbcls=IPython.terminal.debugger:Pdb

这将打开基本的 ipdb运行到错误时的shell。但是没有制表符补全。

    103
    104 def test_me(some_variable):
--> 105     x = 1 + some_variable
    106     return x
    107

ipdb> so[<tab_here_produces_tab>]

4。 fancycompleter.interact()

按照建议尝试了 fancycompleter.interact() here ,没有运气(pdbipdbpdbppfancycompleter v.0.8 甚至 this patched version。) .

5。 pytest --pdb -s

作为Sergey Voronezhskiy注释,-s 标志可用于 pytest。但是,制表符完成仅部分起作用:如果相同的起始字符有多个选项,它将打印出可能变量的列表。这缺乏快速选择匹配变量之一的能力(例如,在 IPython shell 中可用):

example with the -s flag

有没有办法让变量名选项卡完成工作?我更喜欢 ipdbpdbpp 而不是 Vanilla pdb,但即使是 pdb 的工作解决方案也很好。

最佳答案

这不是最佳解决方案,但总比没有好。也许有人会给出更好的答案。这是我做的

1。别名 emb开始IPython.embed()

1.1。步骤

  • 制作~/.pdbrc包含以下内容的文件 ( ‪C:\Users\<USER>\.pdbrc)
alias emb from IPython import embed; embed()

1.2。简短说明

  • 何时pytest使用 --pdb 运行标志,它启动标准库 pdb,或者如果已安装,则启动 pdbpp。
  • 标准库 pdb文档说:

If a file .pdbrc exists in the user’s home directory or in the current directory, it is read in and executed as if it had been typed at the debugger prompt. This is particularly useful for aliases. If both files exist, the one in the home directory is read first and aliases defined there can be overridden by the local file.

2。别名的提示文本(可选)

2.1。步骤

  • 已安装 pdbpp ( pip install pdbpp )
  • 制作~/.pdbrc.py 1 文件 ( ‪C:\Users\<USER>\.pdbrc.py ) 包含以下内容

import pdb


class Config(pdb.DefaultConfig):
    def setup(self, pdb):
        print('Use "emb" to enter IPython shell')

2.2。简短说明

pdbpp documentation说如果你创建一个 ~/.pdbrc.pysetup()方法,当你进入调试器时它会被调用。因此,可以创建一个每次输入 pdbpp 时调用的脚本。调试外壳。

1注意:这个有.py扩展名,而第一个与内置 pdb 相关联的扩展名则没有。

3。使用示例

运行 pytest --pdb (与问题中的 .py 文件相同)。从图中可以看出,变量 some_variable 的制表符补全。有效。

tab completion inside IPython shell inside pdbpp

关于python - 如何使用带有选项卡完成功能的 python 调试器运行 pytest?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52626799/

相关文章:

python - 如何模拟类并使用 pytest-mock 控制 py.test 中的返回值?

python - 使用 Cython 在 numpy 网格上评估 C 函数

javascript - swampdragon如何导入js文件?

python - Y 轴和第一个 X 刻度之间的空间

python - 将不同的人脸检测器与 dlib 的地标检测器一起使用

java - IntelliJ 调试 : Suspend whole VM then step on single thread

德尔福 "Internal Error EVA 1528"

javascript - 如何在 SpiderMonkey JSNative 回调中获取 javascript 调用者源行号?

python - 无法让 PyTest 在 VSCode 或终端中运行。未识别任何测试

python - 在多个数据集上运行相同的测试