A variable is a scalar-value (a unique value, not a list of a result-set) that can be reused in different places of your test-suites. Another big advantage of variables is that they are evaluated during the test-suite execution. Suppose that you have a query expecting a date as a parameter and that you want to specify the current date: without a variable, it’s not possible! A variable is only evaluated once: after it has been evaluated, the value is cached and never updated.
The variables are defined at the top of the test-suite (after settings but before the first test) in an element named variables.
As you can understand fom the fragment above, a variable can be evaluated based on different engines.
This engine evaluates one unique sentence of C# and returns the corresponding value. In order to specify this engine use the element script and specify the attribute language to the value c-sharp. Then you’ll be ableto specify your c# sentence in the inner text of this element. Note that thsi sentence shouldn’t start by return and neither end by a semi-column (;).
In this example, the variable named FirstOfCurrentMonth is set to the value returned by the C# script:
This engine evaluates a query and returns the first cell of the first row returned by this query. In order to specify this engine use the element query-scalar and specify. Then you’ll be able to specify a query with the different methods available in the NBi syntax 2.0 to define a query.
In this example, the variable named CurrencyCode is set to the single value returned by the query here under:
This engine retrieves the value of an environment variable.
In this example, the variable named myVar is set to the value of the environment variable named MyEnvVar:
This solution retrieves the value from an external C# assembly. This assembly must contain one or more types implementing the interface IScalarResolver.
In this example, the variable named myVar is set to the value returned by the type MyCustomClass of the assembly myassembly.dll when executing the method Execute() . Optionaly, you can pass some parameters to the type MyType when instantiating it. In this example, the class MyCustomClass has a constructor accepting two parameters (foo, bar).
You can’t use the variables at all places. The usage is limited to the following places:
- Parameter of a query
- In the comparisons (equal, more-than, less-than) for a row-count
- In the operators of predicates of the assertions row-count, all-rows, no-rows, some-rows and single-row
- In the empty and flat file definitions of a result-set
- In the json-source and xml-source elements
- In the rest-api attributes
- In the renaming alteration
- In the data engineering, IO and process tasks
- In the list elements of a sequence
- In native transformations
- In the definition of custom scalars and custom sequences
If you’ve other places, where you think that a variable would be helpful, report it by creating an issue
Notes about the future of variables
Variables will be extented in the next releases. The goal is to let you define them at different places (groups and perhaps tests) and use them at more places.