I use the Microsoft Entity Framework and have some nested entities
GrandParent, Parent, Child
In a C# test I would like to be able to read an array of GrandParents including their descendants from an xml file, e.g. (fake code example:)
[TestMethod]
[DataSource("myTestData.xml")]
public void MyTest(){
List<GrandParent> grandParents = TestContext.GetEntityList(GrandParent.class);
}
<?xml version="1.0" encoding="utf-8" ?>
<TestData>
<GrandParent>
<name>GrandParent_1</name>
<Parent>
<name>Parent_1.1</name>
<Child>
<name>Child_1.1.1</name>
</Child>
</Parent>
</GrandParent>
<GrandParent>
<name>GrandParent_2</name>
<Parent>
<name>Parent_2.1</name>
<Child>
<name>Child_2.1.1</name>
</Child>
</Parent>
<Parent>
<name>Parent_2.2</name>
<Child>
<name>Child_2.2.1</name>
</Child>
</Parent>
</GrandParent>
<TestData>
The above syntax is not supported by the DataSource annotation and I could only find examples for reading flat data from the DataSource. https://msdn.microsoft.com/en-us/library/ms182527.aspx
Reading hierarchical data and converting that data to an entity object tree does not seem to be supported.
Is there an easy way to create an entity object tree from an xml DataSource for the purpose of testing? Maybe something like Unitils (http://unitils.org/tutorial-database.html) in Java, but for .Net? Ideally, the xml file does not need to include all attributes of the entities: only those entity attributes that are used in the test need to be specified.
Related questions
Here is a related question on how to populate test data. Most of the referenced libraries seem to be out of date and I could not find support for xml import of test data: Is there any framework for .NET to populate test data?
Nested xml DataSource: Data driven tests using nested xml data file