24

Before my testing library of choice was unittest. It was working with my favourite debugger - PuDB. Not Pdb!!!

To use PuDB with unittest, I paste import pudb;pudb.set_trace() between the lines of code. I then executed python -m unittest my_file_test, where my_file_test is module representation of my_file_test.py file.

Simply using nosetests my_file_test.py won't work - AttributeError: StringIO instance has no attribute 'fileno' will be thrown.

With py.test neither works:

py.test my_file_test.py

nor

python -m pytest my_file_test.py

Both throw

ValueError: redirected Stdin is pseudofile, has no fileno()

How can I use Pudb with py.test?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yagger
  • 2,975
  • 1
  • 16
  • 21
  • looks like I've found the answer here http://lists.idyll.org/pipermail/testing-in-python/2011-December/004616.html Simply by adding -s flag will not replace stdin and stdout and debugging will be accessible, i.e. `py.test -s my_file_test.py` – yagger Aug 07 '14 at 12:42
  • docs may be also useful here: http://pytest.org/latest/usage.html#setting-a-breakpoint-aka-set-trace – ambi Aug 07 '14 at 12:43
  • You can replace `import pudb;pudb.set_trace()` with `import pudb.b`. So cool :p – smido Oct 08 '21 at 09:32

2 Answers2

29

Simply by adding the -s flag, pytest will not replace standard input and standard output and debugging will be accessible, i.e., pytest -s my_file_test.py will do the trick.

In documentation provided by ambi, it is also said that previously using -s explicitly was required for regular pdb too, but now the -s flag is implicitly used with the --pdb flag.

However, pytest does not implicitly support PuDB, so setting -s is needed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
yagger
  • 2,975
  • 1
  • 16
  • 21
11

There is now an adapter library available to expose a --pudb tracing option similar to the --pdb one. The more general -s option remains a valid solution for manually placed breakpoints from any debugger, of course.

To use, do pip install pytest-pudb, and then execute Pytest via py.test --pudb. Additionally, import pudb; pudb.set_trace() functionality is supported without the need for -s or --capture=no if this adapter is installed.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
amcgregor
  • 1,228
  • 12
  • 29