RobotCode - Variable Not Found

Hi,
I am using the robotCode extension.
In my project, I have a common.resource and I have the test.robot
In test.robot, I am calling the common.resource with some variables.
In test.robot file I am using those variables but in VSCode robotCode raises an error as variables are not found.
In the previous extension as RobotFramework Language Server, it is not assigned an error, but with RobotCode it is.

Do you have any suggestion how should I handle this error? Where should I pass those variables from common.resource to test.robot file ?
Or do you have other idea, how should I define the variables in my project?

Error : Variable ‘${abc}’ not found.robotcode(VariableNotFound)

Thanks
Project:
common.resource

*** Keywords ***
Setup Environment
    ${RANDOM_VALUE}    Generate Random Value
    Set Suite Variable    ${abc}    ${RANDOM_VALUE}
    Set Suite Variable    ${def}    def_${RANDOM_VALUE}

In test.robot file

*** Settings ***
Resource            common.resource
*** Test Cases ***
TC:01 - Verify Test
    Set Test Variable    ${reservation_id}    166198
    Verify Response    ${abc}     ${def}

Note:
even for the Set Suite Variable definition, I am getting the not found error
Set Test Variable ${string} 166198
Variable ‘${string}’ not found.robotcode(VariableNotFound)

Can you edit the post and format code with ``` ?

Where do you call the Setup Environment keyword?

common.resource

*** Keywords ***
Setup Environment
    ${RANDOM_VALUE}    Generate Random Value
    Set Suite Variable    ${abc}    ${RANDOM_VALUE}
    Set Suite Variable    ${def}    ${RANDOM_VALUE}
    

test.robot

*** Settings ***
Resource             common.resource

Test Setup          Setup Test


*** Test Cases ***
TC:01 - Verify Test
    Set Test Variable     ${reservation_id}    166198
    Verify Response     ${abc}     ${def}


*** Keywords ***
Setup Test
    common.Setup Environment

Apart from the VSCode error indication, when you run the test does the error occur?

The test code seems OK for me.

yes when execution the code, it does not occur the error.
Error is only raising while working in VSCode editor.

How do people handle those kinds of issues?
What are the best practices for using random data which comes from the common place or external resource, in tests ?

We are Software Testers, and the tools we use , most of the times are Open Source Software, which is the case for RobotCode. We must constantly question ourselves if the problem we are finding is from the tool we use, in this case the IDE.
I am the maintainer of RIDE, and this kind of error should not be reported, in the sense that variable autocompletion would not know about the ${abc} variable. We have to understand that these IDEs cannot do such advanced inspection of the code to find all the referenced objects. Well, maybe they can, but the effort to program this is too big.

In short, the free tools are here to help us and we must “guess” their limitations.

The best thing that users can do, is to learn to code, and try to solve the problems they find in their favorite tools. This was my path, from Software Tester (which I still am) to maintainer of an IDE.

see here: [BUG] Global variable detection raised error message · Issue #321 · robotcodedev/robotcode · GitHub

2 Likes

Hi Kubilay,

Welcome to the forum!

TL;DR:

Best practice is to announce global/suite variables in *** Variables *** section of common.resource. That way you do not only get those variable-not-found-“issue” solved, but you also tell every human user of that common.resource what variables to expect (supporting modularity and reusability)

From the RFCP syllabus about *** Variables ***:

  • Variables defined in a resource file, however, are accessible in all files that import the resource file directly or indirectly by imports of other resource files. This allows for the sharing of variables across multiple suites or files while maintaining modularity and reusability.

Long Story

The “limitation” is Robot Framework itself. RobotCode aims to make accurate analysis, where as other tools make a guess. The old Robot Framework LSP that you have mentioned did such guesses. Most of the time it was right, sometimes they weren’t. You only knew for sure when code has been executed.

While RobotCode could probably implement a call analysis, it would slow down project analysis (and thus: autocompletion) tremendiously. And it would still not be accurate as there are many ways to intercept and exchange keyword calls. Thus variable completion would become undeterministic. As a result, the “error” message would disappear in the IDE, but the variable completion would become inaccurate.

Imagine you have hundreds of keywords, shared across a huge project and the keywords make a lot of use of magic SUITE and GLOBAL variables. That is hard to maintain, regardless what IDE. That’s why announcing “global” variables is a best practice for IDEs and maintainability:

*** Variables ***
${ABC}    ${NONE}    # unassigned SUITE variable
${DEF}    ${NONE}    # unassigned SUITE variable

*** Keywords ***
Setup Environment
    ${random_value}    Generate Random Value
    Set Suite Variable    ${ABC}    ${random_value}    # assign SUITE variable
    Set Suite Variable    ${DEF}    ${random_value}    # assign SUITE variable

Although Robot Framework is case-insensitive, it is best practice to write variables with higher scope than LOCAL in capital case.

Kind regards,
Markus

4 Likes