The scope of variables in test data can vary depending on where and how they are defined. Variables can have a global, test suite, test case, or local scope.
Global variables are defined outside of any test suite or test case and are accessible throughout the entire test execution. These variables are typically used to set global configurations or values that need to be shared across multiple test cases or test suites.
Test suite variables are defined within a test suite and are accessible to all test cases within that suite. These variables are typically used to define test suite-specific configurations or values.
Test case variables are defined within a test case and are only accessible within that specific test case. These variables are typically used to define test case-specific configurations or values.
Local variables are defined within a keyword or other block of code and are only accessible within that block. These variables are typically used to store temporary values or intermediate results that are needed within a specific section of code.
Global scope
Global variables have a scope that spans across the entire test data, and can be accessed and modified from anywhere within the test execution. These variables are typically set from the command line using the –variable and –variablefile options, but it’s also possible to create new global variables or modify existing ones using the BuiltIn keyword Set Global Variable at any point in the test data.
In addition, built-in variables are also global variables and can be accessed and modified from anywhere within the test execution.
Test suite scope
Variables with test suite scope are accessible anywhere within the test suite where they are defined or imported. These variables can be created within the Variable sections, imported from resource and variable files, or set during the test execution using the BuiltIn keyword Set Suite Variable.
It’s important to note that the test suite scope is not recursive, which means that variables defined in higher-level test suites are not accessible in lower-level suites. To share variables across test suites, resource and variable files can be utilized.
Since variables with test suite scope can be considered global within the test suite where they are used, it’s recommended to follow the same naming convention as global variables by using capital letters for their names.
Test case scope
Variables with test case scope are only visible within the test case and any user keywords used within the test. Initially, there are no variables in this scope, but they can be created using the BuiltIn keyword Set Test Variable at any point within the test case. It’s important to note that calling Set Test Variable outside the scope of a test (e.g., in a Suite Setup or Teardown) will result in an error.
Variables within the test case scope can be considered to be global to some extent. Therefore, it’s generally recommended to use capital letters for the variable names in this scope as well.
Local scope
Both test cases and user keywords have their own local variable scope, which is not visible to other tests or keywords. Local variables can be created within this scope by using return values from executed keywords, and user keywords also receive local variables as arguments.
To avoid confusion with global and test suite scoped variables, it’s generally recommended to use lower-case letters for local variable names.