Conditions - TestlumFramework/Testlum GitHub Wiki

🔀 Conditions in Testlum

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.


🧩 What is a condition?

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.

Syntax:

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

🛠️ Defining and Using Conditions

1. Define a Variable

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>

2. Define a Condition

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"/>

3. Use the Condition in a Command

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 to true, the <mysql> command executes.
  • If false, the command is skipped.

🔗 Combining Conditions with Logical Operators

You can combine multiple conditions using logical operators:

  • && (AND): Executes the command only if both conditions are true.
  • || (OR): Executes the command if at least one condition is true.

Example:

<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 &amp;&amp; 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 because true && false is false.
  • The second <mysql> command will execute because true || false is true.

📘 Learn More


By utilizing conditions, you can create dynamic and intelligent test scenarios that respond to varying data and states.

⚠️ **GitHub.com Fallback** ⚠️