Unit tests for scripts
Test scenarios
Creating a unit test
For each script (services and validators) test scenarios can be defined. After clicking the button Unit tests in the left bar, the tests panel will open. The panel displays the currently defined test cases for the given script. A new test can be added using the button located at the bottom of the panel Add test.

Each scenario has the following to define:
Test name,
Test description,
Activity - whether it is run when clicking the Run all,
Input parameters - the values we want to test for the given script,
Application data,
Output verification - the values we expect for the given input values:
Output parameters for services,
Message keys for validators.


Unit test results
We can run a selected test by clicking the icon
located on the tile with information about the test scenario. Clicking the Run all button will run all active tests.
The test result may be marked green - the tests finished with the expected result - or red - incorrect result.
After running the test a drawer with logs will appear, containing information:
Name of the test that was run,
Logs from the test,
Output value.
When running all tests, the execution time will be the sum of all tests' runtimes. The logs drawer contains information about each previous run, separated by a blank line. The logs drawer can be cleared of logs using the Clear logs.


Application data in unit tests
When creating unit tests, the application allows simulating data present on the application form. In the Application data tab we can add fields that should return a specified value. We refer to the given values using the getValue() function available on the context object.


Errors during tests
If the script contains errors, the test will fail and the error will be logged in the console under the editor. The message will include the reason and the location of the error.

Methods of output verification
Script services
We can verify the output of script services in several ways. They are divided into number comparisons and text comparisons:
Number comparison
>
Greater
>=
Greater or equal
<
Less
<=
Less or equal
==
Numbers are equal
≠
Numbers are not equal
When choosing one of the above comparison operators, keep in mind that script outputs and the values entered for comparison are converted to numbers. If any value fails to convert, the test will end with a negative result.
For example, for data: service output: 6.0, value to compare: 6, comparison: ==, the result is correct. The value 6.0 returned from the script is equal to the value 6 entered in the expected field.



Text comparison
EQ
Strings are equal
NOT_EQ
Strings are not equal
~
Matches the regular expression
!∅
Not empty
Data are compared as text. Numbers are also treated as text.
For example, for data: service output: 6.0, value to compare: 6, comparison: EQ, the result is incorrect. The text value 6.0 returned from the script is not equal to the text value 6 entered in the expected field.

Script validators
The output of script validators is verified by selecting the message keys that will be returned in the given test case.
For example, a script validator returns two error keys with their parameters:
pl.error1
pl.error2
For the assertion to pass, both error keys should be present in the list.
If we add extra keys or one of the error keys is missing, the test will fail.


Last updated
Was this helpful?
