10

I am trying to debug my tests using pdb (Python debugger) while running them with 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:

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.

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

Thanks!

Mihai
  • 831
  • 6
  • 13
  • 2
    Check this answer, not sure if it covers your need - https://stackoverflow.com/a/40352901/742501. – Mihnea DB Aug 06 '18 at 14:21
  • Thanks for the answer! I have checked this response and, for my usecase, I would rather want to replicate the behaviour of `nosetests -s ` if it's possible in order to keep the same dev flows. If it's not possible this will be the way to go. – Mihai Aug 06 '18 at 14:24
  • Does this answer your question? [Debugging python tests in TensorFlow](https://stackoverflow.com/questions/40340131/debugging-python-tests-in-tensorflow) – Boris Verkhovskiy Sep 05 '20 at 00:02

3 Answers3

4

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:

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

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"
Andrew Ring
  • 3,185
  • 1
  • 23
  • 28
2

You need to use --run_under:

bazel test --run_under=/usr/bin/pdb //webserver/members/api/tests:test_members_resource
László
  • 3,973
  • 1
  • 13
  • 26
  • 4
    Doesn't work. If I run my (failing) tests without it, the tests fail with `bdb.BdbQuit`. If I run it with `--run_under` then they just pass in `0.1s`. `which pdb` returns `/usr/bin/pdb`. I also tried `pdb3` with the same result. Whether I have a `import pdb; pdb.set_trace()` in my code or not has no effect. – Boris Verkhovskiy Mar 17 '20 at 14:53
0

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. When the interpreter hits the breakpoint, it will open pdb.

Optional but very helpful: Use pudb via

pip install pudb
export PYTHONBREAKPOINT="pudb.set_trace"
# add breakpoint() to your code
bazel run PYTHON_TARGET
M. Gruber
  • 351
  • 3
  • 10