0

I am writing a unit test case to get a list of files from an excel sheet and extract some data from them. following this article : http://www.codeproject.com/Tips/629895/Data-Driven-Testing-by-Excel-Named-Region

[DataSource("System.Data.Odbc", "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\\quovantis.git\\kmu\\InputManagement\\KMU.Factoring.Service.Test\\TestData\\TestCasesReferenceData.xls;", "Sheet2$", DataAccessMethod.Sequential)]
    [TestMethod]
    public void DataDrivenInvoiceCheck()
    {
        int clientId = Convert.ToInt32(TestContext.DataRow["ClientId"]);
        //Validate stuff 
    }

This is working fine as of now. However I want to populate this data for multiple tests in a method using the TestInitialize attribute and am looking for a way to store the data in a List or something and Iterate on it .

for example:

[DataSource("System.Data.Odbc", "Driver={Microsoft Excel Driver (*.xls, *.xlsx, *.xlsm, *.xlsb)};DBQ=C:\\TestCasesReferenceData.xls;", "Sheet2$", DataAccessMethod.Sequential)]
[TestInitialize]
void GetDataFromDataSource()
{
  //Get the Data from the data source store it in a class level variable or something
}

[TestMethod]
void TestMethod1()
{
    //Use Data brought from above (Data Driven Test Case)
}

 [TestMethod]
void TestMethod2()
{
    //Use Data brought from above (Data Driven Test Case)
}
SJMan
  • 1,547
  • 2
  • 14
  • 37

1 Answers1

0

Well, you sort of give the answer yourself: 'Get the Data from the data source store it in a class level variable or something'.

This global variable approach seems to work.

[TestClass]
public class Example1Test
{
    public int GlobalIntTest = 42;

    [TestMethod]
    public void MyTestMethod()
    {
        //do something with the global variable
    }
}