RED icon indicating copy to clipboard operation
RED copied to clipboard

"Variable is used, but not defined" message despite variable beeind defined using "set suite variable" in suite/test setup

Open lesnake opened this issue 4 years ago • 4 comments

In the following test suite, the ${bar} and ${baz} variables are defined in suite/test setup, but, in the test case, they are flagged as used but not defined by RED.

*** settings ***
Suite Setup     foo suite setup
test setup      foo test setup

*** keywords ***
foo suite setup
    ${bar} =    set variable    bar
    set suite variable    ${bar}

foo test setup
    ${baz} =    set variable    baz
    set suite variable    ${baz}

*** test cases ***
foo bar
    log to console    \nfoo ${bar}

foo baz
    log to console    \nfoo ${baz}

I think this is a bug because the suite and test setup execution cannot be avoided, thus the variable is always forcefully set when the test case is beeing run.

I expect the error message not to be displayed in test cases. The case of a sub keyword using the test setup defined variable is trickier because one keyword can have different call path (this case is not represented in the above code snippet)

lesnake avatar Sep 09 '20 13:09 lesnake

This is recurring issue coming from limitations of dynamic nature of Robot Framework language. Very closely related (or the same) issues are: #7, #68, #195, #268, #365 and possibly more. Have a look here: https://github.com/nokia/RED/issues/195#issuecomment-375589938 for more in-depth explanations.

michalanglart avatar Sep 09 '20 15:09 michalanglart

Thanks for the explanations, here is a review of the issues you suggested me to read.

#7: is different should generate the error message in testB because tests must be independant from each other. In my opinion it is bad practice. I restrict the use of "set suite variable" to setup related keywords.

#68: set suite variable does not occur in the suite setup or test setup, which is the key point in my request.

#195: Is almost the same, but

  • occurs across different files with no explicit inclusion of the init.robot in environment.robot, so I expect to have the error message
  • occurs in a subkeyword of the keyword used as suite setup, so it is acceptable for me to have the error message because that sub keyword can be used in an unpredictable way, which may not include the test setup.

#268: Is amost the same but author does not use the set suite variable in test/suite setup I think that the following trick will make me happy. Variables intended to be used as suite global shall be in the variables section. Why not ?

*** Variables ***
${A}    ${A}

#365: Same issue.

lesnake avatar Sep 10 '20 09:09 lesnake

hmm

*** Variables ***
${A}    ${A}

triggers an error at runtime. Not acceptable.

lesnake avatar Sep 10 '20 11:09 lesnake

Well, this wouldn't:

*** Variables ***
${A}    \

or even equivalent:

*** Variables ***
${A}

This is basically creating a variable with SUITE scope and empty value when this suite would start. Then the setup would change it.

michalanglart avatar Sep 11 '20 07:09 michalanglart