Environment variables
You can use environment variables in CAT configuration files.
Why environment variables?
There are two main use-cases for using environment variables in CAT project file or other loaded files:
- hiding sensitive information, such as passwords, secrets or entire connection strings
- parametrizaion
Hiding secret stuff is clear (you don’t want to have passwords in plain texts, in Git, …). What about parametrization? It allows you to have queries like this:
Query: |
SELECT COUNT(*) as Count
FROM Operation.SecurityCheck
WHERE Terminal = %TERMINAL_NUMBER%
AND ...... // abbreviated
This way you can have e.g., separate Azure DevOps pipelines for each Airpor terminal, sharing same test definitions.
Syntax
To use an environment variable, refer to it in the configuration with this syntax: %YOUR_ENVIRONMENT_VARIABLE_NAME%
.
Names of environment variables in CAT configuration files ARE case sensitive.
CAT does the replacement only if an environment variable with such name exists. So don’t be afraid all will fail when you have WHERE PilotName LIKE '%SMITH%'
in your query. CAT just ignores that because you don’t have any environment variable named ‘SMITH’.
Where can I use environment variables?
Almost everywhere. E.g., you can hide the entire connection stirng into a variable. Or part of it:
Connection string: '%AERO_CONNECTION_STRING%'
# or
Connection string: >
Host=localhost;port=5432;database=aero_operations;CommandTimeout=10;
User ID=%TESTING_USER%;Password=%TESTING_PASSWORD%;
You can use them in SQL/DAX statements in your queries. You can parametrize parts of names of tests, use them in description, just wherever you need.
CAT replaces environment variables not only in the main .cat.yaml
file, but also in all YAML data source / test definitions loaded from external sources. CAT can load both data source definitions and test definitions from YAML, MS EXCEL, database, … - the replacement of environment variables is done there too.
Troubleshooting
When CAT does not replace environment variable(s), you migt encounter various errors, such as invalid connection string or invalid query syntax.
In those cases, first check for typos and case-sensitivity. If all is OK, verify your process can see the environment variable. E.g., using PowerShell:
$Env:TESTING_USER
If it does not return anything, your session is not aware of the environment variable - and therefore CAT cannot see it either. If it is a new variable, you might need to restart your computer. If you are in Azure DevOps, your environment variables must be properly mapped. Until the above syntax ($Env:YOUR_VARIABLE_NAME
) does not return anything, CAT will not see it.
Tip
Useful trick (but not suitable for secrets) is to define an environment variable “temporarilly”. In PowerShell, you can define it just for your session:
$Env:MY_NEW_VARIABLE = 'something'
Other sessions do not see it and it is gone when your session ends.