August 29, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Data Driven Testing with Visual Studio Team Suite

  • September 22, 2006
  • By Daniel Gartner
  • Send Email »
  • More Articles »

After writing the tests, I implemented the Add() method (see code below) to fulfill the requirements of the tests and then ran them again.

public int Add(ContactInfo ci)
{
   ContactInfoValidator civ = new ContactInfoValidator(ci);
   int contactId = -1;
   if (civ.Validate())
   {
      contactId = this.DataManager.Insert(ci);
   }
   return contactId;
}

When the data-driven unit test is executed, it will run once for each row of its data source. Data-driven tests can be set to execute either sequentially or randomly by changing the value of the "Data Access Method" unit test property in the Test Manager view, or by modifying the DataSource attribute (see Figure 10). If the test fails for any of the rows in the data source, the entire unit test will show that it failed. Conversely, the unit test will only pass if it passed for all of the rows in its data source.



Click here for a larger image.

Figure 11: Unit Tests Passed

You can view the details of a test by double-clicking on the result in the Test Results pane. This shows the test results (pass/fail) for each row in the data source.



Click here for a larger image.

Figure 12: Data-Driven Unit Test Results

Data-Driven Web Testing

In addition to creating data-driven unit tests to test the business logic of the application, I would like to go one step further and perform data-driven Web testing to test the Web interface. I have created a simple Web form that has one text box for each property of the ContactInfo object.

Figure 13: Contact Manager Web Form

When the user adds a new contact, the ContactInfo object is constructed and its properties are populated and passed to the ContactnfoManager's Add() method that was tested above. If the contact is successfully added, a label on the Web form displays a success message. If the contact is rejected, the label displays a failure message. I could test this functionality by manually entering data and clicking save myself, but that seems like it would be wasted effort. Instead, I am going to use the same technique displayed above to automate testing with a data-driven Web test.

The steps you will follow for the Data-Driven Web Testing process are as follows:

  1. Create the Web Test.
  2. Record a browser session.
  3. Add a data source to the Web test.
  4. Bind form post parameters to the data source.
  5. Add validation rule to verify if this test passes or fails.
  6. Set the run settings to execute once for each row in my data source.
  7. Run the Web test.

Creating a Data-Driven Web Test

Create a new Web test by clicking "New Test" under the "Test" menu item.

Figure 14: Creating a New Test

From there, you will be prompted to select the type of test to create. Select "Web Test" and click OK.



Click here for a larger image.

Figure 15: Creating a Web test

This loads a browser window with an additional pane called the "Web Test Recorder" that is used to record Web testing sessions.



Click here for a larger image.

Figure 16: Web Test Recorder

Because all I am testing is the "Add" functionality of the Web form, I populate the text boxes with test data and click the "Add Contact" button. This is all that needs to be recorded for this test. After clicking the "Stop" button on the "Web Test Recorder," the details of the Web test are displayed.

As you can see from the Web test details, any values entered in the text boxes during the recording are stored under the "Form Post Parameters" of the Web test. If I ran the Web test again, it would execute the recorded Web session, submitting all the values that are stored in the form post parameters to the corresponding form fields.

Figure 17: Form Post Parameters

I want this test to use the data from the InvalidContactInfo table used in the unit test example to verify that the Web form rejects all invalid contact info. To do this, click the "Add Data Source" icon, enter your connection information, and click OK. Then, you will be prompted to select the tables to add as the data source. Select the InvalidContactInfo table.

Figure 18: Add Data Source Button

Now that you have added the data source to your Web test, the next step is to bind the form post parameters to their corresponding values in the data source. This is accomplished by modifying the properties of the form parameters. For each of the form post parameters, change the "Value" setting to the corresponding column of the data source.



Click here for a larger image.

Figure 19: Binding Form Post Parameters to Data Source

For instance, the tbName form post parameter should be bound to InvalidContactInfo.Name, tbAddressLine1 should be bound to InvalidContactInfo.AddressLine1, and so on. When completed, all of the form post parameters should be bound to their corresponding column in the data source.



Click here for a larger image.

Figure 20: All Form Post Parameters have been bound

The final step in creating this data-driven Web test is to define the success criteria for the test by creating a simple Validation Rule that looks for the text "Cannot add invalid contact info into the system" after the save button is clicked. Validation rules can be used to check form fields, text on a page, request times, required attribute values and required tags, and are used to verify whether or not a Web test has passed or failed. More information on Validation Rules is available here.

Now that the Web test data bound and ready to go, it's time to execute it. Select the "Run Test (Pause Before Starting)" option from the Play button.

Figure 21: Running the Data-driven Web test

This is done because you want to set the test run to execute once for each row in the data source before it executes. Click the "Edit run settings" link and then check the "One run per data source row" radio button in the Web Test Run settings.

Then, click the Play button to execute the data-driven Web test. The test will run once for each row in the data source, and will use the validation rule to check whether or not that run has passed or failed. Detailed run results are available under the "Details" tab.



Click here for a larger image.

Figure 22: Web Test Results

To increase or decrease the number of runs that are executed, simply add new rows to the data source. No new code modifications are required.

Conclusion

The integrated testing tools provided by Microsoft Visual Studio Team Suite can be used to drastically improve the overall quality of software developed by your organization. This article has examined data-driven unit and Web tests and how they can be used to create more effective tests and improve developer efficiency during the testing process.

About the Author

Dan Gartner is a Senior Consultant with Crowe Chizek and Company, LLC and lives in Chicago, IL. He specializes in designing and developing applications for rapidly evolving organizations using Microsoft technologies.





Page 2 of 2



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel