Welcome, guest | Sign In | My Account | Store | Cart

Robotframework (http://code.google.com/p/robotframework/) is a tool used to run functional tests against a variety of targets. Tests are organized in the form of keyword tsv or html files, which map input parameters to keyword-argument methods in the test suite. Robot includes a fairly advanced logging mechanism, which is cool -- until you try to debug anything. Debugging is made difficult because robot steals stdin and stdout when it is run, which means bye-bye debugging in the terminal. rpdb solves this in a KISS simple way.

Python, 22 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import pdb

IO = lambda s: (s.stdin, s.stdout)

def rpdb(F):
    """
    robot python debugger -- usage:
    @rpdb
    def keyword_method(self, arg1, arg2, ...):
        # stuff here ...
        rpdb.set_trace() # set breakpoint as usual
        # more code ...
    """
    setattr(rpdb, 'set_trace', pdb.set_trace)
    builtinIO = IO(sys)
    def _inner(*args, **kwargs):
        robotIO = IO(sys) # robot has hijacked stdin/stdout
        pdb.sys.stdin, pdb.sys.stdout = builtinIO
        retval = F(*args, **kwargs)
        sys.stdin, sys.stdout = robotIO
        return retval
    return _inner

The recipe is pretty self explanatory. Before robot's __init__ can be called the decorators must wrap the kwarg methods. Since stdin/stdout have not been stolen yet, we save them to a variable inside the decorator's scope. This forms a closure; and right before calling the method we intend to debug, rpdb switches out stdin/stdout temporarily returning control to the terminal. Once debugging is completed, stdin/stdout are returned to robot. Happy debugging all.

2 comments

Christopher Prinos 9 years ago  # | flag

There's an even simpler way, just use the following:

import pdb; pdb.Pdb(stdout=sys.__stdout__).set_trace()

and it will work by forcing output to the python's original output stream.

guo chunyan 8 years, 1 month ago  # | flag

"import pdb,sys;pdb.Pdb(stdout=sys.__stdout__).set_trace()" did not make debug working in robot framework. the error is following: ERROR Error in file '/repo/root/robot/ATS/TEST/test.txt': Importing test library '/repo/root/robot/ATS/TEST/get_var.py' failed: BdbQuit Traceback (most recent call last): File "/repo/root/robot/ATS/TEST/get_var.py", line 8, in <module> pdb.Pdb(stdout=sys.__stdout__).set_trace() File "/opt/robot/Python-2.7/lib/python2.7/bdb.py", line 53, in trace_dispatch return self.dispatch_return(frame, arg) File "/opt/robot/Python-2.7/lib/python2.7/bdb.py", line 91, in dispatch_return if self.quitting: raise BdbQuit