0

This is my Data Source

[DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|Data Directory|\\Input.xml", "testcase", DataAccessMethod.Sequential), TestMethod]

When I execute this, I get the below 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.

The XML File is located in the project's folder location. How can I prevent this error?

sushain97
  • 2,752
  • 1
  • 25
  • 36
Mano KJ
  • 11
  • 3
  • Check the steps given in this answer http://stackoverflow.com/questions/23469100/how-to-run-a-test-many-times-with-data-read-from-csv-file-data-driving/25742114#25742114 . There is not much difference between how XML and CSV files are added and used in Coded UI tests. – AdrianHHH Feb 24 '15 at 09:40
  • Does the DataSource really say `, "|Data Directory|\Input.xml", `? I believe that `\I` is not a valid character in a string and it should provoke a compile time error. – AdrianHHH Feb 26 '15 at 14:41

2 Answers2

0

Instead of above code use this

[DeploymentItem("Project_Name\\Input.xml"), DeploymentItem("Input.xml"), TestMethod,
 DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\Input.xml", "datarow", DataAccessMethod.Sequential)]

use below code to get TestContext:

 public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }
        private TestContext testContextInstance;
fthiella
  • 48,073
  • 15
  • 90
  • 106
Hina Ahuja
  • 126
  • 1
  • 3
0

use below xml with named as LoginInfo.xml and In LoginInfo.xml(Right click on LoginInfo.xml---->Properties) properties set Copy Always in copy to output Directory.

<xml>
  <authentication>
    <username>Biren</username>
    <password>BirenPassword</password>
  </authentication>
  <authentication>
    <username>Viren</username>
    <password>VirenPassword</password>
  </authentication>
  <authentication>
    <username>Bond</username>
    <password>BondPassword</password>
  </authentication>  
</xml>

use below code in Class to check it

 [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML", "|DataDirectory|\\LoginInfo.xml", "authentication", DataAccessMethod.Sequential), DeploymentItem("LoginInfo.xml"), TestMethod]

public void RunningXML()
{   string username=TestContext.DataRow["username"].ToString();
    string password = TestContext.DataRow["password"].ToString();
    Console.WriteLine("userName  :" + username);
    Console.WriteLine("passWord  :" + password);
}
Biren
  • 11
  • 2