Create Data-Driven Tests
Data-driven testing is a procedure when you repeat the same test scenario with different input parameters and then verify the result with the given output values. This example shows how to create a data-driven test case.
Assume you have a data.json
file with data objects (test cases) that consist of two input values (name
and comment
) and a value that specifies the expected result (resultText
).
[
{
"name": "John Heart",
"comment": "I love TestCafe!",
"resultText": "Thank you, John Heart!"
},
{
"name": "Olivia Peyton",
"comment": "TestCafe is awesome!",
"resultText": "Thank you, Olivia Peyton!"
}
]
To create data-driven tests, iterate through the test cases, call the test method at each iteration and write test actions and assertions that use test case parameters.
import { Selector } from 'testcafe';
const dataSet = require('./data.json');
fixture `Data-Driven Tests`
.page `https://devexpress.github.io/testcafe/example/`;
dataSet.forEach(data => {
test(`Enter '${data.name}'`, async t => {
await t
.typeText('#developer-name', data.name)
.click('#tried-test-cafe')
.typeText('#comments', data.comment)
.click('#submit-button')
.expect(Selector('#article-header').textContent).eql(data.resultText);
});
});
Tip
You can store test cases in objects, arrays, Maps, Sets, etc., and iterate through them as you need. You can place them in a test file or import from .json
, .js
, .ts
, etc.
A test case can include multiple tests:
dataSet.forEach(data => {
test(`Check one thing`, async t => {
// ...
});
test(`Check something else`, async t => {
// ...
});
});
You can also introduce conditional actions that depend on test case parameters:
if(data.shouldClick)
await t.click(page.button);