We can Read Data from CSV file just as below and utilize them in the Test cases.
If we require to read data from Multiple CSV file or Different CSV files, We can just change the file name in @"|DataDirectory|\data\data.csv", "data#csv" in the below DataSource Attribute to the file name from which we wanted to read the data and pass to test case.
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Purush_CSV
{
[TestClass]
public class Purush_ReadDataCSV
{
private TestContext testContext;
public TestContext TestContext
{
get { return
testContext; }
set { testContext = value; }
}
[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV",
@"|DataDirectory|\data\data.csv", "data#csv", DataAccessMethod.Sequential)]
public void
Purush_Read()
{
string a = TestContext.DataRow["ResultsColumn"].ToString();
// Will be reading value from ResultsColumn in CSV File which is
located in Data Folder under this solution
}
}
}
0 comments: