我可以在 Bazel 测试中使用 Python 调试器吗

Can I use Python Debugger In Bazel Test

我正在尝试使用 pdb(Python 调试器)调试我的测试,同时 运行 使用 bazel 调试它们。

这是我的样本测试:

class TestMembersResource(TestCase):

    def test_get(self):
        response = self.client.get('/api/v1/members/')
        import ipdb; ipdb.set_trace()
        self.assertEqual(response.status_code)

当我尝试使用 bazel test ... 运行 它时,我得到以下输出:

Traceback (most recent call last):
    File "/root/.cache/bazel/_bazel_root/ae988d93859d448ae36776fcb135b36c/execroot/__main__/bazel-out/k8-fastbuild/bin/webserver/members/api/tests/test_members_resource.runfiles/__main__/webserver/members/api/tests/test_members_resource.py", line 22, in test_get
    self.assertEqual(response.status_code, 200,
    File "/root/.cache/bazel/_bazel_root/ae988d93859d448ae36776fcb135b36c/execroot/__main__/bazel-out/k8-fastbuild/bin/webserver/members/api/tests/test_members_resource.runfiles/__main__/webserver/members/api/tests/test_members_resource.py", line 22, in test_get
    self.assertEqual(response.status_code, 200,
    File "/usr/lib/python2.7/bdb.py", line 49, in trace_dispatch
    return self.dispatch_line(frame)
    File "/usr/lib/python2.7/bdb.py", line 68, in dispatch_line
    if self.quitting: raise BdbQuit
BdbQuit

没有 pdb 一切都非常顺利。

有没有办法获得交互式 shell 并使用带有 bazel 测试的标准 pdb 命令?

谢谢!

您需要使用--run_under:

bazel test --run_under=/usr/bin/pdb //webserver/members/api/tests:test_members_resource

如前所述,您可以使用 --run_under 标志来执行此操作。请务必注意,您需要为 python 安装指向 pdb.py。要查找指向的位置,您可以执行以下操作:

检查 python 版本的安装位置。这应该使用 python2.7 或 python3.6 之类的东西,而不仅仅是 python 或 python3,因为它们通常只是符号链接。

$ which python3.6
/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6

请注意,这是二进制文件所在的位置,而我们要指向一个库文件。为此,将最后一个 bin 替换为 lib,并指定所需的文件,如下所示:

/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py

现在您可以 运行 您的目标如下:

bazel run --run_under="/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/pdb.py"