Creating Data-Driven Tests in MS Test

For my current client, I was looking for a way to create a data-driven unit test in MSTest.  Basically, you can define a test once and then define a data source that provides multiple rows of data to be sent through the same test.  That way, you don’t have to write redundant tests.
I had done this previously, but I couldn’t remember how it was done.  I’m writing this post in order to remind myself and hopefully help someone out there.
1.  Create a Test Method
The first step is to make sure you have a test method.  You can do this by making sure that in your test project you have at least one class decorated with the [TestClass] attribute and a method with the [TestMethod] attribute. 
[TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
}



When creating this test class, make sure to keep the TestContext property as it is defaulted.  You will be using this when coding your test.


private TestContext _testContext;
 
/// <summary>
///Gets or sets the test context which provides
///information about and functionality for the current test run.
///</summary>
public TestContext TestContext
{
get
{
return _testContext;
}
set
{
_testContext = value;
}
}



2.  Add a Test Data File

For the purposes of this explanation we will be using a simple .csv file (TestData.csv).  Make sure to add a text file, preferably at the root of your project with that extension.  In the file, you will want to add data.  I have added three columns per row:  Operand1, Operand2, and ExpectedValue which represents the expected result of adding the two operands together.  You can add as many rows of data to check all of the boundary conditions that express your intentions for your unit test.

image

Right-click on the file and select Properties to set the Build Action to Content and the Copy to Output Directory to Copy Always as shown below.  This will ensure that the file is copied to the Bin folder when running your unit tests.

image

3.  Open the Test View from the Test menu.

From the Test menu, select the Windows –> Test View option as shown below.

image

Once you open the Test View, you should see a list of the test methods that are available.  When a test is highlighted, you will see the properties for that particular test in the Properties pane as shown below. 

image

4.  Assign Test File as Test Data Source

Find the row for Data Connection String in the Properties pane and click on the button to the far right with the ellipses (…).  That should open the following dialog where you will be presented with an option to set up a database, csv file, or xml file.

image

The database option should be self explanatory.  It is like any other database connection, and the most popular option is to use a simple MS Access DB or SQL Express database that you can include with your project.

The other two options are file based.  The CSV file will be explained in further detail below.  The main difference between a CSV and XML file is that you can have multiple test data sets in the XML file while only one set of data would be accessible for a CSV.  So, if you need to set up data for multiple tests, you would need to maintain multiple files with the CSV option.  In the XML option, you could just maintain one with multiple sections within the XML file.

Select the CSV File option and click Next.

5.  Select Your CSV File

At this point, you will be presented with a dialog to select a CSV file.  Click on the (…) button and select the location of the text file that you created for your test.

image

Once selected, the dialog will show the contents of your CSV file as a data grid to allow you to examine the contents to make sure that the data is what you were expecting.

image







Click the Finish button once you have confirmed the file contents.  You will notice that the properties have been updated to reflect the change.

image

In addition, Visual Studio will add a [DataSource] and [DeploymentItem] attributes to your original test method as shown below with the same information.  In the future, you can set these attributes up on a test without needing to use the Properties wizard for selecting a Data Source.

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv", DataAccessMethod.Sequential), DeploymentItem("Demo.DataDrivenTests\\TestData.csv"), DeploymentItem("TestData.csv"), TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
}




6.  Add Code to Reference Data Source

Once you have added your test data to the test method, you will want to access it within the body of your unit test method.  This can be done by referencing the DataRow property of the TestContext class-level variable.  I have created a simple test to use the values to assert that the Add method of a simple Calculator class works as shown below.  Because a DataRow column returns an object, you will need to do some casting to the appropriate data types.

[DeploymentItem("Demo.DataDrivenTests\\TestData.csv"), DeploymentItem("TestData.csv"), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData.csv", "TestData#csv", DataAccessMethod.Sequential), TestMethod]
public void Add_when_two_numbers_returns_proper_result()
{
//arrange
var operand1 = System.Convert.ToInt32(TestContext.DataRow["Operand1"]);
var operand2 = System.Convert.ToInt32(TestContext.DataRow["Operand2"]);
var sut = new Calculator();
 
//act
var result = sut.Add(operand1, operand2);
 
//assert
var expectedValue = System.Convert.ToInt32(TestContext.DataRow["ExpectedValue"]);
Assert.AreEqual(expectedValue, result);
}



7.  Run the Test

You can run the test as you would any other in MS Test.  The result will look similar to other tests.

image

However, if you right-click on the result row in the Test Results pane, and select View Test Results Details as shown below…

image




…you will see the detail of the results to see which data combinations failed and why.

image

Hope this helps someone else out there.