Search code examples
c#unit-testingdata-driven-tests

XML Data Driven Unit Test with nested Nodes


I am not able to find something on the internet for the last hours. The situation is the following: I want to test a parser I have written, for this I have the desire to write a data driven unit test. The XML I have looks like the following:

<Test>
   <ParseTest>
      <Case>
         <uri>somestring</uri>
         <key>somestring</key>
         <value>somestring</value>
      </Case>
      <Case>
         <uri>somestring</uri>
         <key>somestring</key>
         <value>somestring</value>
      </Case>
   </ParseTest>
</Test>

Test is my root node for the whole class. The ParseTest should be the root for each method, so I want to access per run one case node with its childs

My testing code is:

[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",@"PATH\name.xml","ParseTest",DataAccessMethod.Seqential)]
public void ParseTest()
{
   //Arrange       
      m_testContext.DataRow["uri"].ToString();
   //Act
   //Assert
}

Problem with it is, that the Framework does not find the correct childnodes. The file is found correctly. I run the test via “Run test“ in VS.


Solution

  • Following this example the additional ParseTest is redundant (or the nested Case tags are). As far as I have understood, the test framework will load all tags with the name ParseTest and run a test for each (given that you passed "ParseTest" as table name). Not knowing how you access the data from within your test I'd guess that the XML should like something like

    <Test>
       <ParseTest>
          <uri>somestring</uri>
          <key>somestring</key>
          <value>somestring</value>
       </ParseTest>
       <ParseTest>
          <uri>somestring</uri>
          <key>somestring</key>
          <value>somestring</value>
       </ParseTest>
    </Test>