Conditions - TestlumFramework/Testlum GitHub Wiki
In Testlum, the condition
command allows you to define expressions that evaluate to boolean values. These conditions can control whether certain test steps are executed or skipped, enabling dynamic and flexible test scenarios.
A condition
is a command that evaluates a boolean expression using the Spring Expression Language (SpEL). The result can be referenced in other commands to determine their execution.
<condition comment="Description of the condition"
name="CONDITION_NAME"
spel="SpEL expression"/>
-
comment
: A brief description of the condition. -
name
: A unique identifier for the condition. -
spel
: The SpEL expression to evaluate.
First, create a variable that retrieves data, such as the quantity of a specific book in stock:
<var comment="Retrieve 'History of Time' book quantity" name="HOT_AMOUNT">
<sql dbType="MYSQL" alias="SHOPIZER">
<query>
SELECT amount
FROM books
WHERE book_name = 'History of Time'
</query>
</sql>
</var>
Next, define a condition that checks if the quantity is below a certain threshold:
<condition comment="Check if book quantity is less than 25"
name="DB_RESULT"
spel="{{HOT_AMOUNT}} < 25"/>
Apply the condition to control the execution of a command:
<mysql comment="Execute command if condition is met"
alias="SHOPIZER"
file="expected_1.json"
condition="DB_RESULT">
<query>SOME_QUERY</query>
</mysql>
- If
DB_RESULT
evaluates totrue
, the<mysql>
command executes. - If
false
, the command is skipped.
You can combine multiple conditions using logical operators:
-
&&
(AND): Executes the command only if both conditions aretrue
. -
||
(OR): Executes the command if at least one condition istrue
.
<condition comment="First condition is true" name="firstCondition" spel="1 == 1"/>
<condition comment="Second condition is false" name="secondCondition" spel="1 == 10"/>
Using these conditions:
<mysql comment="Attempt INSERT with AND operator"
condition="firstCondition && secondCondition"
alias="MYSQL_0"
file="expected_10.json">
<query>
INSERT INTO news (id, newsname, newsnumber, active, createdat)
VALUES (2, 'News2', 2, true, '2023-04-21 10:48:26.096000')
</query>
</mysql>
<mysql comment="Attempt INSERT with OR operator"
condition="firstCondition || secondCondition"
alias="MYSQL_0"
file="expected_11.json">
<query>
INSERT INTO news (id, newsname, newsnumber, active, createdat)
VALUES (3, 'News3', 3, true, '2023-04-21 10:48:26.096000')
</query>
</mysql>
- The first
<mysql>
command will not execute becausetrue && false
isfalse
. - The second
<mysql>
command will execute becausetrue || false
istrue
.
By utilizing conditions, you can create dynamic and intelligent test scenarios that respond to varying data and states.