Reading test data, data-driven testing

Last edited on

Overview

The following examples demonstrate reading test data for data-driven testing, etc.

Test data file

The test data file used by the following examples:

col1,col2
row1col1,row1col2
row2col1,row2col2
testdata.csv

JavaScript

function main()
{
    // Read generated output:

    // VARIANT #1:
    //
    // Looks for the file in Test Case Resources,
    // then Test Suite Resources:
    dataset = testData.dataset("testdata.csv");

    // VARIANT #2:
    //
    // Looks for the file in Test Case Resources,
    // then Test Suite Resources:
    //dataset = testData.dataset(findFile("testdata", "testdata.csv"));

    // VARIANT #3:
    //
    // Looks for the file in Test Suite Resources:
    //dataset = testData.dataset(squishinfo.testCase + "/../shared/testdata/testdata.csv");

    for (var row = 0; row < dataset.length; row++) {
        value_col1 = testData.field(dataset[row], "col1");
        value_col2 = testData.field(dataset[row], "col2");
        test.log("Next row:");
        test.log("  " + value_col1);
        test.log("  " + value_col2);
    }
}
test.js

Python

def main():
    # Read generated output:

    # VARIANT #1:
    #
    # Looks for the file in Test Case Resources,
    # then Test Suite Resources:
    dataset = testData.dataset("testdata.csv")

    # VARIANT #2:
    #
    # Looks for the file in Test Case Resources,
    # then Test Suite Resources:
    #dataset = testData.dataset(findFile("testdata", "testdata.csv"))

    # VARIANT #3:
    #
    # Looks for the file in Test Suite Resources:
    #dataset = testData.dataset(squishinfo.testCase + "/../shared/testdata/testdata.csv")

    for index, row in enumerate(dataset):
        value_col1 = testData.field(row, "col1")
        value_col2 = testData.field(row, "col2")
        test.log("Next row (%s):" % index)
        test.log("  %s" % value_col1)
        test.log("  %s" % value_col2)
test.py

A More Complex Example

This example (partially consisting of pseudo code, so it cannot be executed actually) demonstrates a more complex case - reading some login information from the test data file and using it:

Test script:

import names
def main():
    # Read generated output:
    dataset = testData.dataset(findFile("testdata", "testdata.csv"))
    for row in dataset:
        username = testData.field(row, "username")
        password = testData.field(row, "password")

        login(username, password)
        do_something_else()

def login(username, password):
    # This will give an error because the object names
    # used here do not exist; this is just an example:

    test.log("Logging in with user '" + username + "' and password '" + password + "'")
    type(names.Username_LineEdit, username)
    type(names.Password_LineEdit, password)
    clickButton(names.Login_Button)

def do_something_else():
    test.log("Doing something else...")
test.py

Test data file:

username,password
user1,password1
user2,password2
user3,password3
testdata.csv