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
norpython -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
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With