Passing a python variable to a python file which is run from the robot file

I am running a python file in my .robot file using the below format. I have a python test file (testrun.py) and a variable file( common_variables.py).

Content of the common_variables.py is:

test_username = "user123"

Content of the testrun.py is:

from Resources import common_variables
myTestUsername = common_variables.test_username
print(myTestUsername)

Content of my .robot file is

*** Settings ***
Documentation Tests using Python and Robotframework
Library Process
Variables Resources/common_variables.py

*** Variables ***
{myTestName} {test_username}
${TESTPATH} testrun.py

*** Test Cases ***
Testing Variable
| | {myTestUserame}= | Set Global Variable | {myTestName}
| | {result}= | run process | python | {TESTPATH} | -v | {myTestName} | | Log to console | hello, world. {myTestName}

I need to pass the value to the “myTestUsername” variable in the testrun.py from the common_variables.py file. How can I do that?
This code | | ${result}= | run process | python | ${TESTPATH} | -v | ${myTestName} fails to execute.
This line of code | | Log to console | hello, world. ${myTestName} works fine and prints the value of the variable from common_variables.py in the PyCharm’s terminal.

I am very new to robotframework and any help from this community is highly appreciated.

back-up a bit and ask yourself “Why am I doing this?” You can pass variables on the robot command line using the -V, you can specify variables in a *yaml, so why are you attempting to retrieve a variable from python?

--variable myTestUsername:userx

Thank you Steve for your suggestion, I was able to make it work.

On a different problem, I am trying to a similar test in robotframework which is calling a python file using Process. It fails to call the python process on Windows machine whereas it works fine on Mac.
If I run the test.py independently it works.
The 2nd line in the test case also works, which prints the “Hello World” text in console.

Content in my Robotfile is like this:

*** Settings ***
Documentation Sample Test
Library Process

*** Test Cases ***
Test
| | {result}= | run process | python | test.py | | {result}= | log to console | Hello World

Any suggestion on why it fails on Windows will immensely help me. Thank you!

Sharing the actual error message where / how it fails is usually a very good idea to share to get some help.

Also, the code you shared has wonky formatting to not sure that makes things even harder to say anything.

Only thing i can even guess what could be a problem is that you are trying to run “python test.py”, but will the working directory be where the test.py is actually located ? How about if you provide cwd parameters to run process ? https://robotframework.org/robotframework/latest/libraries/Process.html#Process%20configuration

1 Like