skjcyber
skjcyber

Reputation: 5957

Can I use dynamically generated XML file in Data Driven Testing

I am trying to use an dynamically generated XML file with Data Driven test. I am using Visual Studio 2010 and .NET 4.0 .

[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", @"|DataDirectory|\StudentData.xml", "Student", DataAccessMethod.Sequential)]

This StudentData.xml file is being generated by code and I am not deploying this file. I have written the code for generating StudentData.xml file in TestInitialize() method, also the file is being saved at current working directory.

Whenever I am going to run the test method it is throwing error:

The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library.
Error details: Object reference not set to an instance of an object.

What I think is before creating the file, the framework is trying to access the file. So, How can I use this XML file with unit test? Also, can I use variable names in Attributes?

Thanks for your help

Upvotes: 2

Views: 1462

Answers (1)

skjcyber
skjcyber

Reputation: 5957

I didn't gey any reply from last 17 hours. As it was important for me, so I tried to explore other possibilities.

Finally I made it to work. Below is the simple solution (I am posting this because it might help other begineers like me, in future).

I have just moved my XML file generation code to [ClassInitialize] method instead of TestInitialize() and it worked fine.

Also, below is the order that methods will be run is:

1.  Methods marked with the AssemblyInitializeAttribute.
2.  Methods marked with the ClassInitializeAttribute.
3.  Methods marked with the TestInitializeAttribute.
4.  Methods marked with the TestMethodAttribute

Check Here for more

Thanks...

Upvotes: 3

Related Questions