Babu
Babu

Reputation: 5

How to run python file in robot framework tests?

Here is the below script file, I want to run in robot tests

import os

def ps_kill():
    os.system(' command')

ps_kill()

And below is my test

*** Settings ***
Library    SeleniumLibrary
Library    Process

*** Test Cases ***
Test case 1
    [Documentation]    Running a command
    [Tags]    Cur


    Launch py File
        ${result} =    run process   python   /path/ps_kill.py

After ran the test it just pass but not run the script.

Upvotes: 0

Views: 3769

Answers (2)

Ramzi Hosisey
Ramzi Hosisey

Reputation: 823

try :

*** Test Cases ***

Launch py File
    ${result} =   evaluate   os.system('echo hello world')

if you want to validate it : you can add print as below:

*** Test Cases ***

Launch py File
    ${result} =   evaluate   print(os.system('echo hello world'))

the idea is that evaluate is the shortest way to run python in robot framework Starting from Robot Framework 3.2, modules used in the expression are imported automatically so there is no need to import the module

Upvotes: 1

Bence Kaulics
Bence Kaulics

Reputation: 7281

There is a much simpler way to run commands from a Robot Framework test, it is by using the OperatingSystem library. This library offers similar functionality as the import os does in Python.

In your case these three keywords that could be used:

An example:

*** Settings ***
Library     OperatingSystem

*** Test Case ***
Test case 1
    ${rc}   ${output} =     Run and Return RC and Output    echo "My string printed by a command"    # Any command could be called here.
    Log    ${rc}
    Should Be Equal As Integers     ${rc}   0    # Check if command execution was succesful.    
    Log    ${output}    # Log output of the command.
 

This would give the following output:

enter image description here

Upvotes: 1

Related Questions