简体   繁体   English

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

[英]Can I use Python Debugger In Bazel Test

I am trying to debug my tests using pdb (Python debugger) while running them with bazel.我正在尝试使用 pdb(Python 调试器)调试我的测试,同时使用 bazel 运行它们。

This is a sample test I have:这是我的示例测试:

class TestMembersResource(TestCase):

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

When I try to run it with bazel test... I get the following output:当我尝试使用bazel test...我得到以下 output:

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

Without pdb everything works pretty smooth.没有 pdb 一切工作都非常顺利。

Is there a way to get an interactive shell and use the standard pdb commands with bazel test?有没有办法获得交互式 shell 并使用标准 pdb 命令和 bazel 测试?

Thanks!谢谢!

你需要使用--run_under

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

You can do this using the --run_under flag, as mentioned. 如上所述,您可以使用--run_under标志执行此操作。 It's important to note that you need to point to the pdb.py for your python install. 请务必注意,您需要指向python安装的pdb.py。 To find where to point to, you can do the following: 要找到指向的位置,您可以执行以下操作:

Check where your python version is installed. 检查python版本的安装位置。 This should be using something like python2.7, or python3.6, not just python or python3, as those are frequently just symlinks. 这应该使用类似python2.7或python3.6的东西,而不仅仅是python或python3,因为它们通常只是符号链接。

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

Note that this is where the binary is located, while we want to point to a library file. 请注意,这是二进制文件所在的位置,而我们希望指向库文件。 To do so, replace the last bin with lib, and specify the desired file, something like this: 为此,请使用lib替换最后一个bin,并指定所需的文件,如下所示:

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

Now you can run your targets like this: 现在您可以像这样运行目标:

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

Alternatively to using bazel's --run_under , you can also just set a breakpoint() (builtin function, no import needed) anywhere is your code and just do a normal bazel run .除了使用 bazel 的--run_under ,您还可以在代码的任何地方设置一个breakpoint() (内置 function,无需导入),只需执行正常的bazel run When the interpreter hits the breakpoint, it will open pdb .当解释器到达断点时,它将打开pdb

Optional but very helpful: Use pudb via可选但非常有用:使用pudb via

pip install pudb
export PYTHONBREAKPOINT="pudb.set_trace"
# add breakpoint() to your code
bazel run PYTHON_TARGET

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM