setting global variables in suite if not existing

10,628 views
Skip to first unread message

AndreasEK

unread,
Jun 8, 2010, 7:43:36 AM6/8/10
to robotframework-users
Hi,

I'm sure this can be done somehow, but how? At one robot installations
there are *many* batch and shell scripts to invoke various tests. Now
for some tests some variables are missing and I don't want to modify
all scripts (not even with a variable file). I was trying to acheive
this with an __init__.txt but failed. How do you test for the
existance of a variable in Set Variable If?

***Settings***
Suite Setup Set CMSVU Variables

***Keywords***
Set CMSVU Variables
Set Variable If not ${RESOURCES} ../../src/main/robot/resource
Set Global Variable ${RESOURCES}
Set Variable If not ${CONTEXT} web/html
Set Global Variable ${CONTEXT}

Thanks,

Andreas

Taylor, Martin

unread,
Jun 8, 2010, 7:57:10 AM6/8/10
to Andreas...@gmx.de, robotframework-users
In Robot Framework variables that are set at run time, from the command line, from variable files or from argument files, override any settings of the same variable name that may be in a resource file. I use this technique to set all the default values for my variables in a resource file and then do selective overrides from the command line for a given test run. In this way you don't need to check at run time if a variable is set, as you were doing in the example below.

Cheers,
Martin

> --
> You received this message because you are subscribed to the
> Google Groups "robotframework-users" group.
> To post to this group, send email to
> robotframe...@googlegroups.com.
> To unsubscribe from this group, send email to
> robotframework-u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/robotframework-users?hl=en.
>
>

AndreasEK

unread,
Jun 8, 2010, 8:13:42 AM6/8/10
to robotframework-users
Hi Martin,

On Jun 8, 1:57 pm, "Taylor, Martin" <cmtay...@ti.com> wrote:
> In Robot Framework variables that are set at run time, from the command line, from variable files or from argument files, override any settings of the same variable name that may be in a resource file.  I use this technique to set all the default values for my variables in a resource file and then do selective overrides from the command line for a given test run.  In this way you don't need to check at run time if a variable is set, as you were doing in the example below.

Thanks for your help! I know the priorities of variables as explained
in [1]

"Variables set during the test execution either using return values
from keywords or built-in keywords Set Test/Suite/Global Variable
always override possible existing variables in the scope where they
are set. In a sense they thus have the highest priority, but on the
other hand they do not affect variables outside the scope they are
defined."

And I'm worried about that they "in a sense ... have the highest
priority". Combined with the warning in Set Global Variable, so that
it should be used with care.

Despite of that ... how do you test for the non-existence of a
variable with Set Variable If?

[1] http://robotframework.googlecode.com/svn/tags/robotframework-2.1.3/doc/userguide/RobotFrameworkUserGuide.html#variable-priorities

Andreas

Pekka Klärck

unread,
Jun 8, 2010, 8:33:19 AM6/8/10
to Andreas...@gmx.de, robotframework-users
2010/6/8 AndreasEK <Andreas...@gmx.de>:

>
> I'm sure this can be done somehow, but how? At one robot installations
> there are *many* batch and shell scripts to invoke various tests. Now
> for some tests some variables are missing and I don't want to modify
> all scripts (not even with a variable file). I was trying to acheive
> this with an __init__.txt but failed. How do you test for the
> existance of a variable in Set Variable If?

You probably need to do something like this (untested):

${status} ${message} = Run Keyword And Ignore Error Variable
Should Exist ${RESOURCES}
Run Keyword If "${status}" == "FAIL" Set Global Variable
${RESOURCES} ../../src/main/robot/resource

If you need to do that multiple times, creating a new user keyword is
probably a good idea:

Set Variable If It Does Not Exist [Arguments] ${name} ${value}
${status} ${message} = Run Keyword And Ignore Error
Variable Should Exist ${name}
Run Keyword If "${status}" == "FAIL" Set Global Variable
${name} ${value}


Using `Run Keyword And Ignore Error` like this is slightly confusing,
especially when the expression for `Run Keyword If` then need to
compare the status as a string. Should we add `Run Keyword And Return
Status` keyword that would execute the specified keyword and return
True or False depending on did the keyword pass or not? That would
make the above example somewhat more clear:

Set Variable If It Does Not Exist [Arguments] ${name} ${value}
${exists} = Run Keyword And Return Status Variable Should
Exist ${name}
Run Keyword Unless ${exists} Set Global Variable ${name} ${value}

Cheers,
.peke

AndreasEK

unread,
Jun 9, 2010, 7:49:27 AM6/9/10
to robotframework-users
Hi Pekka,

thanks for your help. That worked :) Only caveat was, that I had to
escape the variable. Finally working solution looks like this:

Set CMSVU Variables
Set Global Variable If It Does Not Exist \${RESOURCES} ../../src/
main/robot/resource
Set Global Variable If It Does Not Exist \${CONTEXT} /web/html

Set Global Variable If It Does Not Exist [Arguments] ${name} $
{value}
${status} ${message} = Run Keyword And Ignore Error Variable
Should Exist ${name}
Run Keyword If "${status}" == "FAIL" Set Global Variable ${name} $
{value}

Andreas
Reply all
Reply to author
Forward
0 new messages