Since last few days I was working on Data driven unit testing. I used Excel spreadsheet as data source. Although it decreases the redundant code in my test method (instead of writing all test data in test method and calling same function everytime) but on the other hand I think it is making my Unit test complicated(with configuration Settings, parsing data from Excel, unexpected exceptions..etc). Can anyone please shed some light on the pros and cons of each approach. For better understanding I am writting below a sample test method with and without data source:
With Data Source
[TestMethod]
[DataSource("CompanyAddressInfo_DataSource")]
[DeploymentItem("CoreUnitTests\\CompanyUnitTests\\CompanyTestData.xlsx")]
public void CompanyAddressInfo_GetItem_Test()
{
if (TestContext.DataRow["Company_No"] != DBNull.Value)
{
bool expected = false;
bool actual=false;
if (TestContext.DataRow["Expected"].ToString() == "Y")
expected = true;
long Company_No = Convert.ToInt64(TestContext.DataRow["Company_No"]);
CompanyAddressInfo obj = CompanyAddressInfo.GetItem(Company_No);
if (obj != null)
{actual = (obj.Company_No == Company_No);}
Assert.AreEqual(expected,actual);
}
}
Without Data Source
[TestMethod]
public void CompanyAddressInfo_GetItem_Test()
{
bool actual=false;
long Company_No;
Company_No = 20;
CompanyAddressInfo obj = CompanyAddressInfo.GetItem(Company_No);
actual = (obj.Company_No == Company_No);
Assert.AreEqual(true,actual);
Company_No = 23;
CompanyAddressInfo obj = CompanyAddressInfo.GetItem(Company_No);
actual = (obj.Company_No == Company_No);
Assert.AreEqual(false,actual);
..............so on
}
Please note that above mentioned code is just an example. Also, another alternative to data driven unit testing I thought could be to make a generic function for calling dll and pass test data to this function (Hence reducing the redundancy).