Reputation: 689
I am trying to set test variable
in Test template
. I don't understand why I am seeing this error. If I run without Test template
it works perfectly fine. Please help
*** Settings ***
Documentation Suite description
Test Template Templete keyword
*** Test Cases ***
Test case1
set test variable @{FileTypes} JPEG
Test case 2
run keyword pass execution tes
*** Keywords ***
Templete keyword
[Arguments] @{kw} &{args}
run keyword and ignore error @{kw} &{args}
Below is the result in cmd line
Test title | FAIL |
Variable '@{FileTypes}' not found.
------------------------------------------------------------------------------
Test case 2 | PASS |
tes
------------------------------------------------------------------------------
Below is the result in log.html for the first test case.
Upvotes: 0
Views: 7107
Reputation: 495
When passing a variable as an argument, only the variable value is transferred to the keyword. Because of this you are actually giving the Robot []
instead of the list variable name. To avoid this, you need to escape the variable when passing it as an argument with \
.
So in brief your keyword should look like this
Set Test Variable \@{FileTypes} JPEG
The rest of the script can remain as is, you can use the list @{FileTypes}
normally after the keyword has been executed.
Note that whatever you add to the list, there's going to be an empty dictionary value as the last list value due to how you have the &{args}
dict in the keyword call.
Upvotes: 1
Reputation: 20067
Roughly speaking, because when the keyword gets to the case, it tries to expand the variable @{FileTypes}
to its members, so it can pass them to the keyword Templete keyword
as arguments.
And it fails because there's no such variable defined at this point.
I've found a workaround for you - but it isn't pretty.
Instead of passing a variable's value to the keyword, you can pass a reference to the variable itself - even if it's not defined yet. So the case could be:
Test case1
set test variable @FileTypes JPEG
, and it creates a list variable inside the keyword.
But - as there's the @{kw} &{args}
- it also has a member that's an empty dict. The value of the created variable becomes this:
['JPEG', {}]
If you remove the &{args}
inside the template keyword, then it's what you'd expect - a single member; but then you can't use that keyword with named arguments.
If you call it (in the case) with a normal (scalar) variable identifier ($
) instead of the list one (@
):
Test case1
set test variable $FileTypes JPEG
, then because of the &{args}
the framework throws an exception:
Setting list value to scalar variable '${FileTypes}' is not supported anymore. Create list variable '@{FileTypes}' instead.
If you drop the &{args}
the exception goes away; but then you can create only scalar variables, not list ones.
==> I'd reevaluate the chosen approach if I was in your place.
Upvotes: 3