Add Randomness to Automated Tests

Question
How should you automate cases that exercise functionality in a variety of ways?

Answer
Write several workflow-based functions or methods and randomly inject them in your tests.

Comments
It’s important to add an element of randomness in your automated tests. This holds true for both data and operations. For example, use data-driven techniques to switch up the data used by tests.

Example
The example below demonstrates a data-driven approach to testing a login feature.

import { t } from 'testcafe';

fixture `login`
    .page `www.example.com`;

const testCases = [
    {
        name:     'Valid User',
        email:    'valid@example.fake',
        password: 'pass'
    },
    {
        name:     'Invalid User',
        email:    'invalid@example.fake',
        password: 'fail'
    }
];

async function performLogin (email, password) {
    await t
        .typeText('#email', email)
        .typeText('#password', password)
        .click('#submit');
}

for (const user of testCases) {
    test('test ' + user.name, async t => {
        await performLogin(user.email, user.password);

        // Confirm valid user can login.
        await t.expect('#username').eql('Valid User');

        // Confirm invalid user can't login.
        ...
    });
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s