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

with Process Library, I have found it important to give the full path of the process being executed.
Why are you setting result twice?
You have what I think are 2 RF statements on one line, using the old-fashioned vertical separator (or “pipe”) syntax. Why are you coding that way?
Your “log to console” works fine and then you are setting result to that?

   ${result}=   Run Process    /usr/local/bin/myapp  ${service_address}  some_literal_argument     stdout=stdout.txt  stderr=stderr.txt
   Log Many  ${result}
   Should Be Equal As Integers   ${result.rc}    0    msg=Failed with rc ${result.rc}
   Should Be Empty  	${result.stderr}
   Should Contain    ${result.stdout}    Something

See the stdout and stderr parameters at the end of Run Process?
They can help you diagnose problem. Jani Mikkonen mentions the cwd (set your current working directory) parameter, which would also be a good thing to understand. I am executing tests on Mac and Linux, however, not Windows and so I can easily envision platform-related idiosyncrasies with Windows command PATH.

To me, the big realization was that the execution environment of my RF Run Process is NOT as simple as popping one of my own shells; i.e. you cannot make assumptions about the execution environment, env variables - they won’t be there the way you want them to be.