C # Unit Testing - XML ​​Datasource containing multiple tests

I am new to unit testing system. Using VS 2010, I use XML as my data source.

Suppose my XML looks like this:

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

Thus, a test group can contain many tests. It would be inefficient to split them into separate xml files. For simplicity, pretend that param1 is int and param2 is another int, and my test would have to check that param2> param1.

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

I have not found a solution yet. The data source may not be designed to test data this way.

+7
source share
1 answer

Using NUnit, you can do the following:

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

You can do some test cases:

 [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 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); } 
+13
source

All Articles