# Variables

### Overview

In Vexdata, variables play a crucial role in enhancing the flexibility and reusability of test cases. By introducing variables, test cases can be designed to adapt dynamically to different data sets or conditions without the need for direct modification of the test case itself.

### When to use variables :

1. To filter data: Example: Let's say I receive data daily into a set of five tables. My test case validates these five tables. Instead of validating the entire dataset, I want to validate only a specific batch of data. In this case, I can use variables to define the criteria for filtering the data, such as a specific date range or a batch identifier.
2. Migrating the same test cases to different environments: When migrating test cases from the development environment to the QA environment, the database schema name may differ between the two environments. To accommodate this difference, variables can be used to store the schema name dynamically. By using variables, the test cases can be easily adapted to work in different environments without the need for manual modification of the schema references in each test case.
3. To execute test cases on different set of tables in a same environment. Instead of changing the test case, we can set variables for table name.

## Static Variable -&#x20;

Static variables, in the context of VexData.io, refer to those that retain their value throughout the execution of a test case. These variables are typically declared and initialized at the beginning of the test case or provided at the beginning of a 'test run,' and they maintain their value until the end.

#### Example

Variable name : id

Initial value : 20  ,  The initial value will be used by default. When executing test cases as a batch, user can over write the variables and the new overwritten value will be used for that test run.

For eg - ${YYYY-MM-DD hh-mm-ss} will be replaced by actual date at runtime on daily run.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FBsT3i6DOxgqQIPPzthSn%2Fimage.png?alt=media&#x26;token=621f4b48-d2c2-4e46-99bf-2c287b41196d" alt=""><figcaption><p>Create a static variable</p></figcaption></figure>

####

## **Project Level variable**

* &#x20;These variables are defined at the project level.
* &#x20;These variables can be accessed by all test cases within the project. They are ideal for constants or configurations that remain the same throughout the project's lifecycle.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2Fax95DnLmQF0G2VQDj9Ix%2Fimage.png?alt=media&#x26;token=cde7ad06-d610-46c8-9838-d667bc9d535a" alt=""><figcaption></figcaption></figure>

#### Steps to create a project level variable :&#x20;

1. **Navigate to the Project** : Select the project where user need to create a project level variable.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FdV5blX3nuBRYz85VRVP4%2Fimage.png?alt=media&#x26;token=acd2bb30-0b7b-4b73-be37-aefa44d5570e" alt=""><figcaption></figcaption></figure>

2. **Click on the Project Level Icon**: Locate and click on the project level icon to access variable settings within the desired project.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FpCzUBGetJhk4ksnOAuH9%2Fimage.png?alt=media&#x26;token=2e8bc3a3-b44d-4d84-9060-7884a61db18f" alt=""><figcaption></figcaption></figure>

3. **Provide Variable Name and Value**: Enter the desired variable name and its corresponding value in the provided fields. Once entered, make sure to save the changes to finalize the variable creation.

{% embed url="<https://www.loom.com/share/30eeff63601544aa8b30afe58f91953d?sid=21bff5cc-99a8-4cc9-a267-6ac596fbaa98>" %}

* **Test Case Level**: These variables are specific to individual test cases. They can be used to store values that are only relevant within the scope of a single test execution, allowing for more granular control over variable use in different scenarios.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2F9czaQsMe1DzLTQI2kr2n%2Fimage.png?alt=media&#x26;token=5d385259-e452-43b4-9060-7f36846da01f" alt=""><figcaption></figcaption></figure>

#### Steps to Create a Test Case Level Variable

1. Open the test case where you want to add a variable.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FcQbw8lvJlj7ahs84zsAO%2Fimage.png?alt=media&#x26;token=014fe546-3df9-46ac-aa60-7b25515e3f45" alt=""><figcaption></figcaption></figure>

2. Click on the **Variables** the test case editor.
3. Enter the desired variable name and its corresponding value in the provided fields. Once entered, make sure to save the changes to finalize the variable creation.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FIY8Izgm4sXg5ro15JGia%2Fimage.png?alt=media&#x26;token=09e5781e-0d1e-4148-a54d-0bf632e840fa" alt=""><figcaption></figcaption></figure>

These steps allow you to manage and utilize variables tailored to specific test case needs, ensuring flexibility and precision in your testing process.

## **Dynamic Variable** -&#x20;

On the other hand, dynamic variables within the VexData platform are initialized by executing a SQL query. The query is executed as a first step before executing the test case. The SQL query should produce only one value which is initialized to the parameter.&#x20;

To use a variable in a test case, we have to rap the variable name in ${variable\_name}

#### Example

Variable name : id5

Query : `select max(batch_id) from sales_tbl`

To use the variable in the test case, use <mark style="color:blue;">${id5}</mark>

`select max(batch_id) from` <mark style="color:blue;">${id5}</mark>

The variable can be used as a table name, inside a sql query, schema name.&#x20;

{% hint style="info" %}
The SQL query should output only one value.
{% endhint %}

The validate button validates the sql query and ensure the sql query is returning only one value. If the sql query is returning multiple records or multiple columns, the error message is displayed and sql query has to be fixed.

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FHUAzsBdKhG2VsHmQajVh%2Fimage.png?alt=media&#x26;token=6ea0fca1-8c49-45c5-b473-21f0b2a46b26" alt=""><figcaption><p>Create a Dynamic Variable</p></figcaption></figure>

**How to create and use Static and Dynamic Variable in a test case**

{% embed url="<https://vimeo.com/831207546/9803611d77?share=copy>" %}

<br>

<div><figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FBhMi0alZStdwzgfuxOC9%2FScreenshot%202024-12-17%20184622.png?alt=media&#x26;token=2d35f530-b780-4329-988b-160fffd2f2e7" alt=""><figcaption></figcaption></figure> <figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FYJrtZGtFmjbQAlx7d2fw%2Fimage.png?alt=media&#x26;token=bf5b5e16-d356-4adc-a82f-393e861f7c31" alt=""><figcaption></figcaption></figure></div>

## **Dynamic Date Variable**

Vexdata now supports dynamic date expressions directly within variable values. This eliminates the need to manually update date parameters each month or deployment cycle. Users can provide SQL expressions to generate date values which are evaluated at runtime.

**Syntax**

Use the keyword VEXD\_CURRENT\_DATE followed by a valid Spark SQL expression to compute the desired date format:

`VEXD_CURRENT_DATE <spark_sql_date_expression>`

<figure><img src="https://2027008842-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaO8uOTM2rdJeIbVeXK0c%2Fuploads%2FCzMTzw5tfmsJJZA0WNde%2FScreenshot%202025-05-29%20at%208.14.27%E2%80%AFPM.png?alt=media&#x26;token=762ac4d5-2b04-4fb5-ab81-14502e871185" alt=""><figcaption><p>Dynamic Date Variable</p></figcaption></figure>

In the above screenshot, variable name is current\_month and value is&#x20;

**Variable Name :** current\_month

**Variable Value :** VEXD\_CURRENT\_DATE date\_format(current\_date(), 'MMyyyy')

> The expression must be a valid Spark SQL date function, typically wrapped in a date\_format(...) clause to control output.

Examples

| Description               | Expression                                                                     | Result (if run in May 2025) |
| ------------------------- | ------------------------------------------------------------------------------ | --------------------------- |
| Current Month (MMYYYY)    | VEXD\_CURRENT\_DATE date\_format(current\_date(), 'MMyyyy')                    | 052025                      |
| Current Month (YYYY-MM)   | VEXD\_CURRENT\_DATE date\_format(current\_date(), 'yyyy-MM')                   | 2025-05                     |
| Current Month (MMM-YYYY)  | VEXD\_CURRENT\_DATE date\_format(current\_date(), 'MMM-yyyy')                  | May-2025                    |
| Previous Month (MMYYYY)   | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), -1), 'MMyyyy')   | 042025                      |
| Previous Month (YYYY-MM)  | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), -1), 'yyyy-MM')  | 2025-04                     |
| Previous Month (MMM-YYYY) | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), -1), 'MMM-yyyy') | Apr-2025                    |
| Next Month (MMYYYY)       | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), 1), 'MMyyyy')    | 062025                      |
| Next Month (YYYY-MM)      | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), 1), 'yyyy-MM')   | 2025-06                     |
| Next Month (MMM-YYYY)     | VEXD\_CURRENT\_DATE date\_format(add\_months(current\_date(), 1), 'MMM-yyyy')  | Jun-2025                    |
