views:

312

answers:

4

We are looking to run a large amount of data through some unit tests. We would like to define this in some sort of excel spreadsheet or XML document.

Is there away to get the unit testing framework to load this data as input and expectations.

I can foresee this having issues with exception catching. Any comments on this are appreciated as well.

A: 

MBUnit can drive tests from data sources such as XML documents or databases.

Eric J.
A: 

For MSTest, take a look at the DataSource attribute.

Mark Seemann
+1  A: 

nUnit fixtures are just POCO's so you can really set it up however you like using .NET classes. Because it could be a lot of data I would probably set it up in a TestFixtureSetUp which is run once for the entire suite:

[TestFixture]
public class Foo{

  private XmlDocument doc;
  private BarClass bar;

 [TestFixtureSetUp]
  public void FixtureSetUp(){
     doc = new XmlDocument();
     doc.Load("c:\file.xml");
  }

  [SetUp]
  public void SetUp(){
      BarClass = new BarClass();
  }

  [Test]
  public void TestX(){
     Assert.That(BarClass.DoSOmething(doc), Is.Baz);
  }

}
ryber
+1  A: 

Having just spent a few hours looking for something on how to do this (because the data had some formatting embedded in it so I couldn't use a CSV file) I managed to work out how to use XML with MSTest.

This is a small sample. Hope it helps.

Suppose you have a simple class library:

    public class GetStrings
{
    public string RichardIII(string lookup)
    {
        string results;
        switch(lookup)
        {
            case "winter":
                {
                    results =
                        "Now is the winter of our discontent\nMade glorious summer by this sun of York;\nAnd all the clouds that lour'd upon our house\nIn the deep bosom of the ocean buried. ";
                    break;
                }
            case "horse":
                {
                    results =
                        "KING RICHARD III \nA horse! a horse! my kingdom for a horse!\n\nCATESBY \n\"Withdraw, my lord; I'll help you to a horse.\"";
                    break;
                }
                default:
                results = null;
                break;
        }
        return results;
    }
}

So a unit test that will check out part of this method will look like this:

        [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\TestStrings.xml", "Test", DataAccessMethod.Sequential), DeploymentItem("GetStringsTest\\TestStrings.xml"), TestMethod]
    public void RichardIIITest()
    {
        GetStrings target = new GetStrings(); 

        string lookup = TestContext.DataRow["find"].ToString();
        string expected = TestContext.DataRow["expect"].ToString();
        if (expected == "(null)")
            expected = null; 

        string actual = target.RichardIII(lookup);
        Assert.AreEqual(expected, actual);
    }

The xml file TestStrings.xml looks like this:

<TestStrings>

Doug Boone