locked
Multiple data sources for mstest unit test

    Question

  • I need to write a unit test (using C# in VS2008 with MSTEST) that repeats for every test case defined in a csv datasource file, and repeats all that for each test case in another datasource file.

    Is that possible?

    I  could instead include a loop in my test for the second set of test cases (not really happy with that), or I could create a big data source which is the cartesian product of both data sets (open to omissions), or I oculd just write a new test method for each of the second test cases (a lot of code duplication).

    I'm sure I am not the first to have encountered this challenge. Can MSTest accomodate it?

    TIA

     


    Neil Haughton

    Friday, February 10, 2012 9:18 AM

Answers

All replies

  • TestInitialize is the way to go when you want something to be executed before each test, be it a method or another unit test http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.testinitializeattribute(v=vs.80).aspx

    Thanks,

    Anuj


    http://www.anujchaudhary.com

    Saturday, February 11, 2012 12:58 AM
  • Anuj,

    How does that help me achieve what I am setting out to do? I want to iterate through each value in one datasource file, and for each value in that, iterate through all the values in another datasource, executing the test for each data pair. In other words, if the first datasource has the values x,y,z, and the second datasource has the values a,b,c, the test would be executed for each of the data pairs:

    x,a

    x,b

    x,c

    y,a

    y,b

    y,c

    z,a

    z,b

    z,c

    Neil


    Neil Haughton

    Saturday, February 11, 2012 1:58 PM
  • Hi Neil,


    Thank you for posting in the MSDN forum.


    I need to make sure: You want to use multiple datasources for a single unit test, am I right?

     

    We can create two unit tests and have the separate datasource for each. But we cannot have multiple datasource for a single unit test. Because the concept for data-driven is that the unit test runs once per row in the data source where the data points are parameters to the test.

     

    Reference:

    http://social.msdn.microsoft.com/Forums/en-US/vststest/thread/8f05ca17-f751-4ad6-920c-54fa56fc8f28

    If I have misunderstood anything, make feel free to let me know.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Monday, February 13, 2012 10:05 AM
    Moderator
  • Jack,

    Yes, I want to effectively have nested loop of source data, ie multiple datasoures for a single test.

    I can simulate this by having multiple test methods, virtually identical except with x,y or z hardcoded as a test parameter in each one (one test for x, one for y etc) with a common datasource for the other values (a,b,c in my example), but I wanted to make it DRYer.

    This is what I've done, but you can see it is highly repetitive:

      [TestMethod(), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\ParseTimeConfigurationValues.csv"....
            public void DateTimeConverter_ParseTimeConfiguration_HandlesExpectedInputFormats_GBCulture_Format1()
            {
                SetWinTimeFormatThenDoTest((string)tci.DataRow[0], "en-GB", "HH:mm");
            }

      [TestMethod(), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\ParseTimeConfigurationValues.csv"....
            public void DateTimeConverter_ParseTimeConfiguration_HandlesExpectedInputFormats_GBCulture_Format2()
            {
                SetWinTimeFormatThenDoTest((string)tci.DataRow[0], "en-GB", "h:mm");
            }

        [TestMethod(), DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\ParseTimeConfigurationValues.csv"....
            public void DateTimeConverter_ParseTimeConfiguration_HandlesExpectedInputFormats_USCulture_Format3()
            {
                SetWinTimeFormatThenDoTest((string)tci.DataRow[0], "en-US", "HH:mm tt");
            }

    etc.... (lots of them)


    Neil Haughton


    Monday, February 13, 2012 10:16 AM
  • Hi Neil,

    Glad to receive your reply.

    As your reply, it is highly repetitive. But we cannot have multiple datasource for a single unit test in the current VS version. Maybe the next version can achieve it. You could submit the suggestions and ideas: http://visualstudio.uservoice.com/forums/121579-visual-studio.Microsoft engineers will evaluate them seriously, thanks for your understanding.

    Best Regards,


    Jack Zhai [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, February 14, 2012 5:51 AM
    Moderator