Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Python pudb debugger with pytest

Tags:

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()

Any ideas about how to use Pudb with py.test

like image 249
yagger Avatar asked Aug 07 '14 12:08

yagger


People also ask

How to debug pytest with pdb?

Using pytest to start pdb debugger You can use different command options like l (list), a(args), n(next) etc., after entering into pdb prompt. To quit from the pdb prompt, simply press q(quit) and the ENTER key. This will invoke the Python debugger at the start of every test. ii) Enter into PDB prompt on failures.

How do I debug a pytest code?

it's real simple: put an assert 0 where you want to start debugging in your code and run your tests with: Alternatively, if you are using pytest-2.0.1 or above, there also is the pytest.set_trace () helper which you can put anywhere in your test code. Here are the docs.

How do I use PDB in Python debugger?

Using Python debugger within the code. We can use pdb.set_trace() or pytest.set_trace() to invoke PDB debugger and tracing from within the code. When your code stumble across the line with pdb.set_trace() it will start tracing and you can see a pdb prompt in the command prompt.

How do I use pudb in pytest?

To use, pip install pytest-pudb 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.

What are the advantages of using pytest debugger?

The main advantage is you can monitor the state of variables, stop and resume the flow of execution, set breakpoints etc. This post lets you understand how to use the Python debugger features with pytest options and inserting set_trace () statements inside your code.


1 Answers

Simply by adding the -s flag pytest will not replace stdin and stdout 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 explicitly -s was required for regular pdb too, now -s flag is implicitly used with --pdb flag.

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

like image 128
yagger Avatar answered Oct 09 '22 23:10

yagger