How to run python script in robot framework properly

Viewed 40

I'm new to robot framework, I have a python script that I want to run in robot framework but it's not running properly, it fails and the output is empty of the script(the error: '' does not contain 'hello')

the robot framework file:

*** Settings ***
Library    OperatingSystem
Library    Process

*** Test Cases ***

Simple pyhton example
    ${result} =     Run Process     python3   hello.py  -c   arg1  -b  arg2
    Log    ${result.stdout}
    Should Contain    ${result.stdout}    hello 

the py file:

import sys
print("This is the name of the script:", sys.argv[0])
print("Number of arguments:", len(sys.argv))
print("The arguments are:", str(sys.argv))
print("hello world")

1 Answers

First of all your hello.py file needs to contain a function (or a class with methods). Not sure what you want to do with the specific arguments, but the content could look like this:

def hello_world(name, friend):
    return "hello " + name + " and " + friend

Now you may import it as a library into your robot file and call the function:

*** Settings ***
Library    hello.py


*** Test Cases ***
Simple Pyhton Example
    ${result}    Hello World    bitrex    superman
    Log    ${result}
    Should Contain    ${result}    hello

More information: https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#creating-test-libraries

Related