我可以在Bazel测试中使用Python调试器吗 [英] Can I use Python Debugger In Bazel Test

查看:88
本文介绍了我可以在Bazel测试中使用Python调试器吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在使用bazel进行测试的同时使用pdb(Python调试器)调试测试.

I am trying to debug my tests using pdb (Python debugger) while running them with 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 ...运行它时,得到以下输出:

When I try to run it with bazel test ... I get the following 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

没有pdb,一切都可以顺利进行.

Without pdb everything works pretty smooth.

有没有一种方法来获取交互式外壳,并在bazel测试中使用标准pdb命令?

Is there a way to get an interactive shell and use the standard pdb commands with bazel test?

谢谢!

推荐答案

您可以使用--run_under标志进行操作,如上所述.重要的是要注意,您需要在安装python时指向pdb.py.要找到指向的位置,可以执行以下操作:

You can do this using the --run_under flag, as mentioned. It's important to note that you need to point to the pdb.py for your python install. To find where to point to, you can do the following:

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

Check where your python version is installed. This should be using something like python2.7, or python3.6, not just python or python3, as those are frequently just symlinks.

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

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

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:

/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"

这篇关于我可以在Bazel测试中使用Python调试器吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆