Skip to content

Data driven testing#

Author Ville Purhonen
Last Updated 10.12.2024

So far we have focused on user-driven testing where the test cases simulate actions that an actual user would do. Data driven testing is another approach, where the same test is performed multiple times with different parameters. It is specifically useful when the amount of possible input permutations is large, which is always the case when the user can input text in any length. A login window is one typical use case for data driven testing, as you usually have to test things such as wrong/correct username and password, special characters, illegal character length and even something like SQL injection in security testing. Also useful for other rote testing such as checking that UI elements are in place.

Syntax on Robot Framework#

In data driven testing only one test is executed. The repeated test is defined under Settings with the keyword Test Template as shown below

***Settings***
Test Template   <keyword>
The repeated keyword should be some user defined higher level keyword that would usually input text, press accept and then confirm the output.

Data driven testing can be done without any external libraries, but if the amount of parameter inputs starts to fill the screen it is usually more sensible to have the test data in a separate file and use the third party library DataDriver to read it. We will go through two examples of a data driven approach, first without DataDriver and then using it.

***Settings***
Library     Seleniumlibrary
Test Template   Login should fail with wrong credentials

***Keywords***

Login should fail with wrong credentials
    [Arguments]     ${username}     ${password}
    [Setup]     Open Browser    url
    Input Text      identifier      ${username}
    Input Text      identifier      ${password}
    Page Should Not Have    text
    [Teardown]      Close Browser

***Test Cases***            Username        Password
Login with wrong password   foo     demo
Login with wrong user       demo    bar
Login with wrong both       demo    demo
Login with correct both     foo     bar
Empty user      ${Empty}    bar
Empty password      foo     ${Empty}

In the test cases section all the tests are one defined in the Test Template, the names are just for the sake of documentation. The Username and Password on the Test Cases row are also effectively just comments for the sake of clarity, Robot Framework does not read anything next to the headers.
As you can see the script is still readable, but if the amount of permutations grows to tens or even hundreds this becomes incomprehensible. For that sort of case, we will look at an example made utilizing the DataDriver library. Like all other third party libraries, it has to be installed with pip with the command

pip install robotframework-datadriver
This time we will be utilizing the Browser library. It is an alternative to SeleniumLibrary with a bit harder syntax, but more features and faster execution speed. In the example we will check the colour of several UI elements on the Prestashop site that is accessible through internet.

***Settings***

Library     Browser
Library     DataDriver      Data.csv
Suite Setup     Browser Setup
Test Template       Check colour

***Variables***

${URL}      https://fip-AAA-BBB-CCC-DDD.kaj.poutavm.fi/index.php

***Test Cases***

Check colours

***Keywords***

Check colour
    [Arguments]     ${locator}      ${element}      ${true_colour}
    ${bg_colour} =  Get Style   ${locator}  ${element}
    Should Be True      '${bg_colour}' == '${true_colour}'

Browser Setup
    New Browser     firefox
    New Page    ${URL}

The Test Cases just needs one row for some text, the content is irrelevant. What matters is that the Test Template has a user defined keyword as an argumenet, which acts as the test that is run with different parameters. Under the Settings header the DataDriver library has one argument. It needs the location of a CSV file where the test cases and parameters are listed. The CSV is presented below.

*** Test Cases ***;${locator};${element};${true_colour}
Check header colour;xpath=//header[@id='header']/div[2];background-color;rgb(255, 255, 255)
Check wrapper colour;css=#wrapper;background;rgb(246, 246, 246)
Check footer colour;css=#footer;color;rgb(35, 35, 35)
First column of the first row is always just the Test Cases row. It is followed by the parameter names of the test that is referred to in the Test Template row in the robot script. In the following rows the first column is the test case name, which can be left empty if needed. And the following columns are the parameters. Columns can also be used for test case documentation or tagging, see the library documentation for more details.