Define variables


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.

Definition

The variables are defined at the top of the test-suite (after settings but before the first test) in an element named variables.

<variables>
    <variable name="FirstOfCurrentMonth">
      <script language="c-sharp">
        DateTime.Now.AddDays(1 - DateTime.Now.Day)
      </script>
    </variable>
    <variable name="CurrencyCode">
      <query-scalar>
        <![CDATA[select top(1) CurrencyCode from [Sales].[Currency] where Name like '%Canad%']]>
      </query-scalar>
    </variable>
  </variables>

As you can understand fom the fragment above, a variable can be evaluated based on different engines.

C# engine

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:

<variable name="FirstOfCurrentMonth">
  <script language="c-sharp">
    DateTime.Now.AddDays(1 - DateTime.Now.Day)
  </script>
</variable>

Query engine

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:

<variable name="CurrencyCode">
  <query-scalar>
    <![CDATA[select top(1) CurrencyCode from [Sales].[Currency] where Name like '%Canad%']]>
  </query-scalar>
</variable>

Environment variable

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:

<variable name="myVar"/>
  <environment name="MyEnvVar"/>
</variable>

Custom variable

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).

<variable name="myVar"/>
  <custom assembly="myAssembly.dll" type="MyCustomClass">
    <parameter name="bar">10</parameter>
    <parameter name="foo">@myValue</parameter>
  </custom>
</variable>
using NBi.Core.Scalar.Resolver;
using System;

namespace NBi.Testing.Core.Scalar.Resolver.Resources
{
    public class MyCustomClass : IScalarResolver
    {
        private int Foo { get; }
        private DateTime Bar { get; }

        public MyCustomClass(DateTime bar, int foo)
            => (Bar, Foo) = (bar, foo);

        public object Execute() => Bar.AddDays(Foo);
    }
}

Usage

You can’t use the variables at all places. The usage is limited to the following places:

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.