mercredi 3 février 2016

Loop over xml data source in unit test recursively

I am writing a unit test for my api. I am using an XML dataSource to read the data. My sample data source xml file looks something like below -

<Rows>
 <Row>
  <id>Guid1</id>
  <id>Guid2</id>
  <id>Guid3</id>
  <id>Guid4</id>
  <id>Guid5</id>
 </Row>
 <Row>
  <id>Guid6</id>
  <id>Guid7</id>
  <id>Guid8</id>
 </Row>
 <Row>
  <id>Guid9</id>
  <id>Guid10</id>
 </Row>
</Rows>

I want to create a data-driven unit test out of it. Please let me know how can I recursively read the values of 'id' using TestContext.DataRow["id"] ?

My sample code would look something like the below -

// I've defined the [TestMethod], [DeploymentItem] & [DataSource] attributes on top of my API

List<Guid> obj1 = new List<Guid>();
// This is how we do it traditionally :
var id = (Guid)testContextInstance.DataRow["id"];

// How do I loop over these ids? from the datasource?
foreach( var id in ids)
{
   obj1.Add(id);
}

Aucun commentaire:

Enregistrer un commentaire