Home · All Namespaces · All Classes · Grouped Classes · Modules · Functions codeless banner

[Previous: Chapter 2] [Contents] [Next: Chapter 4]

Chapter 3: Data driven testing

The first page of this tutorial gave an example of testing creation of a contact:

    creating_a_contact: function()
    {
        // Start the application
        startApplication( "Contacts" );

        // Open the options menu and choose "New contact"
        select( "New contact", optionsMenu() );

        // Enter some details in the "Name" and "Emails" fields
        enter( "Frank Grimes", "Name" );
        enter( "frank@example.com", "Emails" );

        // Select 'Back' from the softkey menu to commit changes
        select( "Back", softMenu() );

        // We should now be at the contacts list.
        // Verify that we can select the contact we just created.
        select( "Frank Grimes" );

        // We should now be viewing the contact.
        // Move to "Details" tab.
        select( "Details", tabBar() );

        // Now verify that the details screen contains
        // the expected details.
        var text = getText();
        verify( text.contains("Frank Grimes") );
        verify( text.contains("frank@example.com") );
    }

One problem with this test function is that only one set of values is tested. If we want to test additional values, the best way to do it is with QtUiTest's built-in support for data driven testing.

We create a new object, named the same as our testfunction with '_data' appended, and we make the object consist of a series of arrays:

    creating_a_contact_data: {
        simple:          [ "Frank Grimes",        "frank@example.com", "+61 0321 FRANK" ],
        no_email:        [ "Bob Jones",           undefined,          "+61 0321 BOB"   ],
        with_middlename: [ "Jane Middlename Doe", "jmd@example.com",      undefined        ]
    }

testcase.creating_a_contact_data is a table of test data with rows simple, no_email and with_middlename. Each row has 3 columns, and these are passed to the creating_a_contact test function as the arguments name, email, homephone in the example below:

    creating_a_contact: function(name, email, homephone)
    {
        // Start the application
        startApplication( "Contacts" );

        // Open the options menu and choose "New contact"
        select( "New contact", optionsMenu() );

        // Enter details
        enter( name, "Name" );
        enter( email, "Emails" );
        enter( homephone, "Home Phone" );

        // Select 'Back' from the softkey menu to commit changes
        select( "Back", softMenu() );

        // We should now be at the contacts list.
        // Verify that we can select the contact we just created.
        select( name );

        // We should now be viewing the contact.
        // Move to "Details" tab.
        select( "Details", tabBar() );

        // Now verify that the details screen contains
        // the expected details.
        var text = getText();
        if (name != undefined)      verify( text.contains(name) );
        if (email != undefined)     verify( text.contains(email) );
        if (homephone != undefined) verify( text.contains(homephone) );
    }

This test is now much more extensible. New test cases can simply be added as new rows to the testdata table and will automatically be tested without needing to add further logic to the test function.

[Previous: Chapter 2] [Contents] [Next: Chapter 4]


Copyright © 2009 Trolltech Trademarks
Qt Extended 4.4.3