|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer|
Overview of Data-Driven Unit Tests
A data-driven unit test is a unit test that is run repeatedly for each row in a data source.
A common scenario for using data-driven unit tests is the use of multiple input values to test an API. Instead of writing multiple unit tests that call the API, each with a new set of inputs, or creating an array in a unit test and using looping code, you can instead write a single unit test method that exercises the API. You can then retrieve data from the rows of a database table, for example, to pass to successive calls of that test method.
You could use this technique to test an application that is used by different users, each of whom has a different role. For each user, one row in the data source would indicate the expected response, based on role. The test would then test the application by running functionality for each user and verify that the response produced matches the expected response.
Creating Data-Driven Unit Tests
You can create a data-driven unit test in either of two ways:
Use the Properties window and set specific properties on an existing unit test. For more information, see.
Code the test as a data-driven unit test. For more information, see.
Viewing Test Results for Data-Driven Unit Tests
After you run a data-driven unit test, its results appear in the Test Results window. You can display the Test [Results] page by double-clicking the test result, clicking a test result and pressing Enter, or right-clicking the test result and choosing View Test Results Details.
The Test [Results] page displays a table whose rows represent the individual runs of the unit test method. Each row in this table corresponds to a row in the database table from which data was retrieved. This display shows you which sets of input succeeded and which ones failed. You can double-click any one of the rows in this table to see additional details about that iteration of the data-driven test.