How can I debug my python unit tests within Tox with PUDB?

4.3k views Asked by At

I'm trying to debug a python codebase that uses tox for unit tests. One of the failing tests is proving difficult due to figure out, and I'd like to use pudb to step through the code.

At first thought, one would think to just pip install pudb then in the unit test code add in import pudb and pudb.settrace(). But that results in a ModuleNotFoundError:

>       import pudb
>E       ModuleNotFoundError: No module named 'pudb'
>tests/mytest.py:130: ModuleNotFoundError
> ERROR: InvocationError for command '/Users/me/myproject/.tox/py3/bin/pytest tests' (exited with code 1)

Noticing the .tox project folder leads one to realize there's a site-packages folder within tox, which makes sense since the point of tox is to manage testing under different virtualenv scenarios. This also means there's a tox.ini configuration file, with a deps section that may look like this:

[tox]
envlist = lint, py3

[testenv]
deps =
    pytest
commands = pytest tests

adding pudb to the deps list should solve the ModuleNotFoundError, but leads to another error:

self = <_pytest.capture.DontReadFromInput object at 0x103bd2b00>

    def fileno(self):
>       raise UnsupportedOperation("redirected stdin is pseudofile, "
                                   "has no fileno()")
E       io.UnsupportedOperation: redirected stdin is pseudofile, has no fileno()

.tox/py3/lib/python3.6/site-packages/_pytest/capture.py:583: UnsupportedOperation

So, I'm stuck at this point. Is it not possible to use pudb instead of pdb within Tox?

2

There are 2 answers

1
Vivek Gani On

There's a package called pytest-pudb which overrides the pudb entry points within an automated test environment like tox to successfully jump into the debugger.

To use it, just make your tox.ini file have both the pudb and pytest-pudb entries in its testenv dependencies, similar to this:

[tox]
envlist = lint, py3

[testenv]
deps =
    pytest
    pudb
    pytest-pudb
commands = pytest tests
0
Noam Manos On

Using the original PDB (not PUDB) could work too. At least it works on Django and Nose testers. Without changing tox.ini, simply add a pdb breakpoint wherever you need, with:

import pdb; pdb.set_trace()

Then, when it get to that breakpoint, you can use the regular PDB commands:

  • w - print stacktrace
  • s - step into
  • n - step over
  • c - continue
  • p - print an argument value
  • a - print arguments of current function