0

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).

Abzoozy
  • 874
  • 12
  • 26
IFlyHigh
  • 546
  • 2
  • 9
  • 20
  • Take a look at this for an alternative: http://stackoverflow.com/questions/9021881/how-to-run-a-test-method-with-multiple-parameters-in-mstest#13710788 – Kris Vandermotten Nov 19 '13 at 11:01
  • But what exactly is is that you are testing - that the `CompanyAddressInfo.GetItem` function works, or that the underlying data is all there? If the former, you only need to test 3 cases - an undefined Company_No, a valid one, and an invalid one. If the latter, then you're probably not Unit testing. – Stephen Byrne Nov 19 '13 at 11:16
  • @StephenByrne : I am trying to do the former one only(unit testing with valid..invalid and undefined data). But as I said above mentioned is only an example to make my question clear..there are no. of Parameters which I need to check (not only Company no.) and as no. of Parameters increases no. of combinations for test data also increases. Eg. valid Company no but invalid Company adress ..vice versa. – IFlyHigh Nov 19 '13 at 11:44
  • I see what you mean. But you could then consider loading that test data file up front, once, for all tests, as opposed to having to deal with it separately for every test. Have you considered using something like fitNesse? http://fitnesse.org/ - this would alow you to focus on just writing the test harnesses for specific parameterised tests, and leave the provision of the test data as a separate job for QA, etc. – Stephen Byrne Nov 19 '13 at 11:50
  • @StephenByrne ..I understand that there are no. of alternative tools but at this point of time my question is not really what are the alternatives but which of the above approach is better. – IFlyHigh Nov 19 '13 at 12:46
  • ok fair enough, in that case then personally I recommend the second approach, and I would write individual unit tests for the Empty, Valid. Invalid cases. This lets your Unit tests be properly isolated without any dependency on external data. But that's just me :) – Stephen Byrne Nov 19 '13 at 13:30

0 Answers0