Dave
Dave

Reputation: 4328

Debugging robot framework python keyword libraries

For learning purposes I want to follow the execution path in a robot framework python library. Actually the ssh library

What is the best way to do this?

I have looked at debug lib , which seems to provide me with the ability to set a breakpoint and spawn a new shell. However I want to examine the execution flow, the stack and the variable values set. Something like pudb but triggered via pybot. Is this possible?

Upvotes: 15

Views: 25010

Answers (5)

Eiston Dsouza
Eiston Dsouza

Reputation: 404

Just in case someone is using vscode and breaking his head. Then please refer to this: https://forum.robotframework.org/t/run-with-debug-robot-framework-in-vs-code/6555/8

From the thread: settings.json of vscode should have:

"debugpy.debugJustMyCode": false, //to debug dependent libraries
"robotcode.debug.attachPython": true

And then just debug your case from the test panel of vscode (debug button of robotcode)

Upvotes: 0

user8128167
user8128167

Reputation: 7676

If you use the RobotCode Visual Studio Code extension with the following in launch.json you can step into the Python code:

// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
    {
        "name": "RobotCode: Run Current",
        "type": "robotcode",
        "request": "launch",
        "cwd": "${workspaceFolder}",
        "target": "${file}"
    },
    {
        "name": "RobotCode: Run All",
        "type": "robotcode",
        "request": "launch",
        "cwd": "${workspaceFolder}",
        "target": "."
    },
    {
        "name": "RobotCode: Default",
        "type": "robotcode",
        "request": "launch",
        "purpose": "default",
        "presentation": {
            "hidden": true
        },
        "attachPython": false,
        "pythonConfiguration": "RobotCode: Python"
    },
    {
        "name": "RobotCode: Python",
        "type": "python",
        "request": "attach",
        "presentation": {
            "hidden": true
        },
        "justMyCode": false
    }
]

The key parts are that justMyCode must be false and that robotcode.debug.attachPython is enabled as shown above because this is disabled by default.

See https://github.com/d-biehl/robotcode/issues/113

Upvotes: 0

Ramon Medeiros
Ramon Medeiros

Reputation: 2616

You can debug directly in robot files, without creating a keyword:

Evaluate    pdb.Pdb(stdout=sys.__stdout__).set_trace()    modules=sys, pdb

https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#using-the-python-debugger-pdb

Upvotes: 0

IslamTaha
IslamTaha

Reputation: 1564

As I prefer to use ipdb more than pdb, then here is my way to use it with robot

import ipdb; ipdb.stdout.update_stdout(); ipdb.stdout.set_trace()

Hint: For some reason the autocomplete wont be working using pdb nor ipdb so if u care about the autocomplete u need to install pdbpp via pip install pdbpp then add this to your code

import sys
import pdb
for attr in ('stdin', 'stdout', 'stderr'):
    setattr(sys, attr, getattr(sys, '__%s__' % attr))
pdb.set_trace()

Upvotes: 3

Bryan Oakley
Bryan Oakley

Reputation: 385970

You can use pdb with robot. How to do so is documented in the robot framework user guide, in the section titled Using the python debugger (pdb).

The example it gives is to add this where you want to set a breakpoint:

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

Upvotes: 24

Related Questions