Simple C# data driven unit test with csv

In this blog, I wanted to address the problem how we can do data driven testing with csv files in C# unit tests.

We can do looping of for different data sets for sequential data. (Each iteration of test for each row in the data in csv file).

 private TestContext testContext;
 public TestContext TestContext {

 get { return testContext; }
 set { testContext = value; }


For the unit tests in MS Test Frame work, we need to define the testing context,

and the context will return the current context.

Lets see how we can loop taking the example of Calculator class.

Lets assume Calculator class having all methods corresponding to do user actions.

 public UITest Calculator = new UITest();

Lets see how we can use data source, we can do by following code.

 @"|DataDirectory|\data\data.csv", "data#csv", DataAccessMethod.Sequential)]

We can use DataSoruce tag for the data source such as CSV or excel. Its better to use CSV files.

Most often we get error for missing reference. See that you have “System.Data” reference in the project setup.


Lets look at the actual implementation and the data structure in the project explorer.


The data csv contains following for illustration.


Following is the test written.


We can use Test Explorer to execute it which will produce results for each row.

There by we can get insight of which one got failed.TestResults

Closer look at TestStak.White for windows Application Automation

In the context of UI testing of windows based application, White is well known and free framework.

In the visual studio 2015 we can install White with following nuget command.

Install-Package TestStack.White -Version 0.13.3

Once it is installed, we can use the ui finders and methods corresponding .Net Applications.

Let’s see how we can use the methods of TestStack.White to perform our Calculator testing.

Highlighted command Launches the application exe just like we do from run.

App.GetWindow is the method to get the parent window and it is start point of the whole testing flow as we go through we use window.Find methods to find controls in the parent window.

Another highlighted line in the below image is WaitWhileBusy.


There are several methods to do the synchronization in the elements and the properties, which we will be discussing in detail.

White framework extensively uses the finders in the UIA interface of the Microsoft.

Let’s examine following function to get the calculator button by text.


(figure 2)

There are different search criteria’s to find the element in the window.


As looked highlighted above we are using control type to find collection of all buttons and filter them by the control text.

There are several control types TestStack.White supports. Following is snapshot of general .net controls.


We can also use one finder in conjunction with another finder with “AndBy” commands like below.


“Window.Get” will get exactly one UI element and if we want collection of elements we can use


Most of the times GetMultiple will be useful where we want to see what is the collection size, and look at all elements and filter them by our needs.

In the (figure 2) there is mention of ExtensionClass, it is implemented for getting objects with wait.

The regular Get Methods does not include synchronization, in the below you can see how we can get elements with wait.

Below is the example where we are checking for length of the Items and periodically checking if length is zero until timeout is reached. We can also include exception handling and return the proper exception if length is zero.


By using these finders created following functions for clicking buttons and entering text items, selecting listboxes etc. Once all methods are done, its just matter of calling them to execute the tests.

Following is the functions created for calculator functionalities diagram for VS.

code map