Lists in Robot Framework

Introduction

You can create a list with @

*** Settings *** Documentation Creating List *** Variables *** @{versions}= 18 19 20 21

Often you need to determine whether an element belongs to a list or not

With in you can check whether such an object is in the list or not.

This operator exists in Python and other programming languages.

in is separated by one space

$var in $list

Checking membership in a list

Consider the test list.robot let's do a simple check of whether our version PostreSQL is in the list of allowed versions or not. The test will not fail.

** Variables *** ${PG_VERSION} 11 @{versions}= 11 13 *** Test Cases *** Test1 Log To Console ${PG_VERSION} Log To Console ${versions} IF "${PG_VERSION}" in ${versions} Log To Console "IN" ELSE Log To Console "NOT IN" END

robot list.robot

============================================================================== List ============================================================================== Test1 11 .['11', '13'] ."IN" Test1 | PASS | ------------------------------------------------------------------------------ List | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/robot/output.xml Log: /home/andrei/robot/log.html Report: /home/andrei/robot/report.html

If you do not need ELSE, you can remove it or use the Run Keyword If keyword

Test2 Run Keyword If "${PG_VERSION}" in ${versions} Log To Console Version ${PG_VERSION} is OK

Test2 Version 11 is OK Test2 | PASS |

Banner image

Example

Consider the test in.robot let's make not just a check but a mandatory condition that the version be from the list of allowed ones.

Now the test must fail for an unknown version.

*** Settings *** Documentation Testing in operator *** Variables *** @{versions}= 18 19 20 21 ${current_version}= 21 *** Keywords *** Start TestCase Log To Console "Starting Test Case" Compare Versions Log To Console ${current_version} Should Be True $current_version in $versions Finish TestCase Log To Console "Finishing Test Case" *** Test Cases *** Verify that in is working [Documentation] This test case verifies that "in" can be used [Tags] Functional Start TestCase Compare Versions Finish TestCase

robot in.robot

============================================================================== Example :: Testing in operator ============================================================================== Verify that in is working :: This test case verifies that "in" can... "Starting Test Case" .21 ."Finishing Test Case" Verify that in is working :: This test case verifies that "in" can... | PASS | ------------------------------------------------------------------------------ Example :: Testing in operator | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: /home/andrei/tests/Example/output.xml Log: /home/andrei/tests/Example/log.html Report: /home/andrei/tests/Example/report.html

Article author: Andrei Olegovich