Data Driven Testing lets you define parameters in your test and use it from any step. You can define multiple ordered Data Sets and have the test run against each set separately using the Testim CLI.

Using Test Data in your tests

  • Test Data can be added from within a test in the setup step or from an external file sent via CLI.
  • A DataSet is defined by a JS Object literal.
  • Multiple ordered Data Sets are defined by a JS Array literal of objects.
  • Testim CLI custom run parameters override the value of a parameter defined in a DataSet that was previously defined in the Testim Editor.
  • When you run your test from Testim CLI, the test will run multiple times, in order, each time with a different Data Set.

Popular use cases for Data Driven Testing

  • Run a signup test with different data - when you want to test your signup page with different user name and password, you can use Test Data and Conditions to build one test in Testim, that run several times with different username and password negative or positive.
  • Read\inject data from database - when you want to inject data into the database and then pass the data that was generated by the test. To generate the data you can use Testim CLI hooks and after injection of the data was successfully completed you can pass the data to your test.

Data Driven from test editor

Test Data can be added to a specific test in the UI by choosing "Set dynamic data..." in the setup step's properties panel.

1. Create a test

Our test is composed of:

  • Navigation to the login page
  • Set username
  • Set password
  • Click on login button
  • Validate message text

2. Open setup step's properties 

3. Click on "Test Data" and choose "Set dynamic data" 

4. Define the data set in the JS editor

This is the Data set we defined in the example:

return [{
  "username": "tomsmith",
  "password": "SuperSecretPassword!"
},{
  "username": "david",
  "password": "SecretPassword?"
}];

Note: The brackets must remain in the return line.

5. Edit "set username" and "set password" steps

Once the Data set was defined, go back to attach it to the relevant inputs: "Username" and "Password":

  • Edit step by clicking the "Set username" step and open its property panel.
  • Replace the text in "Text to assign" with username parameter.
  • Edit step by clicking the "Set password" step and open the property panel.
  • Replace the text in "Text to assign" with password parameter

6. Run you test

The test run successfully with the first Data Set:

"username": "tomsmith","password": "SuperSecretPassword!"

Note: Running the test from the editor will only run the first data set. When you want to run all the data sets use CLI/scheduler.

Data Driven via CLI

Test Data can be passed to one or multiple tests with CLI using the config file.

  1. Add overrideTestData to the beforeSuite hook 
  2. Pass tests names and data sets (data sets should be in Json format)  

Here is an example of a beforeSuite hook in the configuration file :

beforeSuite: function () {
    return {
       overrideTestData: {                
          "Test 1": {user: "dave", password : "123"},
          "Test 2": {name: "ryan"}                    
        }
    }    
} //add comma here if there are more functions after beforeSuite

Here is the same example with 2 datasets for the first test :

beforeSuite: function () {
    return {
       overrideTestData: {                
          "Test 1": [{user: "michelle", password : "belle"},
                     {user: "paul", password : "walrus"}]
          "Test 2": {name: "john"}                    
        }
    }    
} //add comma here if there are more functions after beforeSuite

Data Driven from an external source

Test data from external sources (e.g CSV, DB etc.) can be passed to one or multiple tests with CLI using the config file.

  1. Add a JavaScript function that loads data from an external source into Json objects. Add this function in the beginning of the config file.
  2. Use overrideTestData in the beforeSuite to pass test names and their data sets.
  3. Install the npm package csvtojson in order to use this functionality. See more details here: https://www.npmjs.com/package/csvtojson

Here is an example :

// JS function that loads data from CSV into Json objects

const csvtojson = require("csvtojson");
function loadCsvFile(path) {
    return new Promise(resolve => {
        csvtojson()
            .fromFile(path)
            .then(jsonObj => {
                resolve(jsonObj);
            })
            .catch(err => {
                console.error("failed to read csv file", err.message);
                resolve([]);
            });
    });
}
beforeSuite: function () {
    return Promise.all([loadCsvFile('./data.csv'),
loadCsvFile('./data2.csv'), loadCsvFile('./data3.csv')])
        .then(([jsonObj, jsonObj2, jsonObj3]) => {
            return {
                BEFORE_SUITE: "BEFORE_SUITE",
                overrideTestData: {
                "Test 3": jsonObj,
                "Test 4": jsonObj2,                        
                "Test 5": jsonObj3,                        
                // You can also pass static datasets to tests
                "Test 6": {name: "ryan"}                    
            }
        }
    });
} //add comma here if there are more functions after beforeSuite

Notes: 

  • Test data provided in the beforeSuite() hook in the config file overrides test data provided in the UI.
  • In the csv file the first row (header row) will contain the data keys and will be used as parameter names, and each row afterwards will contain a dataset of values.

Viewing test runs with different data sets

  1. Go to runs page and go to the suite that you just run.
  2. You can see the run result with the DataSet per test when you hover the "i" icon in the right side.

Test Data can be changed from the CLI for a specific run by using a Custom Run Parameters. The parameters object will be merged into the current DataSet replacing any previously defined parameter.

Setting Cookies

You can also define your cookies with using the reserved 'cookies' parameter. 

Read all about the setting cookies options here

Did this answer your question?