Share via


Using XML File as Data Source for Visual Studio Data Driven Testing

Scenario :
  Using XML file as data source for Data driven testing in Visual studio 2010 Ultimate
Error Observed: 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.

Resolution:
Please use following attributes on the test methods:

[TestMethod]
[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML","|DataDirectory|\\TestDataSource.xml","TestData", DataAccessMethod.Sequential)]
public void DemoTest()
{

}

Make sure to add the XML file as Deployment Item i.e.
1) Add this attribute to the test method 
[DeploymentItem(@"\\<Path>\TestDataSource.xml")]

Or 2) Go to test setting and add the XML file as deployment item Or 3) Right click on the XML file - go to properties of the file and set copy to output directory = copy always