Here I am going to demo data driven unit testing using a CSV file. Ultimately we are going to put test data into a CSV file and use that file data as an input for unit testing.
This article is a continuation of the earlier article "Visual Studio 2010 Tools - To improve code quality". Here I am going to demo data driven unit testing using a CSV file. Ultimately we are going to put test data into a CSV file and use that file data as an input for unit testing.Please refer to my earlier article:http://www.c-sharpcorner.com/UploadFile/sanks/1733/Default.aspx Step 1: View Unit Test CodeHere we have a MathLibrary project having Add, Multiply, Divide and Subtract methods in the CalculateBasic.cs class file. In the MathUnitTest project we have CalculateBasicTest.cs file for unit testing.We are going to do a data driven unit test on the following MultiplyNumbers method.The following is the Unit Test code for the above method.Step 2: Create CSV fileTo add a new CSV file to the Unit Test Project, right-click the MathUnitTest project and click Add -> New Item.Select Text File and name it MultiplyTestData.csv.Step 3: Add data to CSV fileAs shown below the file is created; then enter test data as shown. As shown the CSV file, Number1 and Number 2 are input parameters and Total is the result of multiplication of Number1 and Number2. Step 4: Set CSV file propertyRight-click the MultiplyTestData.csv file and click properties. Select the Copy always option as shown below so that the file is copied to the bin folder after build.Step 5: View Change Attribute of test methodTo use the CSV file as an input for test data:
Printing in C# Made Easy