Since using Ruby the pry gem has always been the first to be added to any of my gemfiles. It was very useful when getting to grips with ruby and also invaluable when it came to debugging.
Recently I started a project which uses Python in its testing stack, being new to python the first thing I googled was “pythons alternative to pry”. A few different options are available but the one I went for and am still using is ipdb. https://pypi.python.org/pypi/ipdb
So how do you use ipdb? Firstly we must install the package using pip (package management system)
$ pip install ipdb
Now set a breakpoint, insert the following into the code where you would like to access the interactive shell and start debugging.
$ import ipdb; ipdb.set_trace()
Once the script has hit this line of code you can start inspecting. Some useful commands to note when inside the shell :
list (list the 11 lines of code surrounding your breakpoint) n (next: continues execution, executes the current statement) s (step:execute and step into function) c (continue: continue to the next breakpoint)
More commands are available and there are plenty of ipdb cheatsheets that can be found online.
I predominately use python to aide my testing; two packages I use a lot to assist with the creation of automated checks are pytest and behave. If you intend to use ipdb with these packages you must turn off the capture output, this needs to be set at execution:
$ pytest tests/tests_one.py -s $ behave tests/features/feature_one.feature --no-capture
I am a big fan of sublime and adding a code snippet for ipdb saves you having to type the set_trace() function over and over. Add the following code to a new snippit in sublime and save as ipdb.sublime-snippet.
<snippet> <content><![CDATA[import ipdb; ipdb.set_trace()]]></content> <!-- Optional: Tab trigger to activate the snippet --> <tabTrigger>ipdb</tabTrigger> <!-- Optional: Scope the tab trigger will be active in --> <scope>source.python</scope> <!-- Optional: Description to show in the menu --> <description>ipdb</description> </snippet>
Hope you find this useful.