8

I am new to the Unit Testing Framework. Using VS 2010, I use an XML as my datasource.

Suppose my XML would look like this:

<testgroup>
  <test>
    <param1>100</param1>
    <param2>200</param2>
  </test>
  <test>
    <param1>333</param1>
    <param2>222</param2>
  </test>
</testgroup>

So a test group could contain a lot of tests. It wouldn't be efficient to break them out in individual xml files. For simplicity, pretend that param1 is an int and param2 is another int and my test would be to verify that param2 > param1.

Is it possible to write a single TestMethod so that it iterates through the various tests from the XML such that the Unit Test Framework will show a test for each?

I haven't found a solution so far. Perhaps the datasource is not meant to be test data driven this way.

Nic
  • 151
  • 1
  • 2
  • 9

1 Answers1

15

Using NUnit, you can do the following:

[TestMethod]
public void TestDerpMethod(int a, string b, bool c)
{
    //...test code...
}

You can do multiple test cases like so:

[TestMethod]
[TestCase(12, "12", true)]
[TestCase(15, "15", false)]
public void TestDerpMethod(int a, string b, bool c)
{
    //...test code...
}

You can also use this method with XML using this method:

<Rows>
    <Row>
        <A1>1</A1>
        <A2>1</A2>
        <Result>2</Result>
    </Row>
    <Row>
        <A1>1</A1>
        <A2>2</A2>
        <Result>3</Result>
    </Row>
    <Row>
        <A1>1</A1>
        <A2>-1</A2>
        <Result>1</Result>
    </Row>
</Rows>

and the C#:

[TestMethod]
[DeploymentItem("ProjectName\\SumTestData.xml")]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
                   "|DataDirectory|\\SumTestData.xml",
                   "Row",
                    DataAccessMethod.Sequential)]
public void SumTest()
{
    int a1 = Int32.Parse((string)TestContext.DataRow["A1"]);
    int a2 = Int32.Parse((string)TestContext.DataRow["A2"]);
    int result = Int32.Parse((string)TestContext.DataRow["Result"]);
    ExecSumTest(a1, a2, result);
}


private static void ExecSumTest(int a1, int a2, int result)
{
    Assert.AreEqual(a1 + a2, result);
}
Codeman
  • 12,157
  • 10
  • 53
  • 91
  • awesome! Just what I was looking for thank you. Your two approaches were very useful. The little thing that I was missing was how you define the DataSource where you have "Row" matching the XML element. I must have missed that part in my earlier reading. [Side note: I can't up your comment. My rep is too low :(] – Nic Jan 12 '13 at 01:04
  • No problem :) Make sure to mark it as answered if it answered your question. – Codeman Jan 12 '13 at 01:05
  • 2
    @Pheonixblade9 Isn't TestCase an NUnit attribute? – Jake May 28 '14 at 15:00
  • ```TestCase``` doesn't exist in MS unit test framework. – Philippe Mar 11 '16 at 11:54
  • @Philippe changed the text to explicitly reference NUnit – Codeman Mar 15 '16 at 22:09
  • Note that since VS2012, the DeploymentItem Attribute is project relative and not solution relative anymore https://stackoverflow.com/questions/16025719/why-does-data-driven-unit-test-fail-in-vs2012-when-it-worked-fine-in-vs2010 – baywet Sep 14 '17 at 15:19