Question

I'm having an issue with my Datasource for my unit test. I'm wanting to keep the records in XML. As far as I can tell this is supported but I keep getting this error "The unit test adapter failed to connect to the data source...".

I have set up my app.config as follows:

<configuration>
  <configSections>
    <section name="microsoft.visualstudio.testtools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </configSections>
  <connectionStrings>
    <add name="PersonTestData" connectionString="Dsn=XML Files;dbq=PersonTestData.xml;defaultdir=.\; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
  </connectionStrings>
  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="PersonTestData" connectionString="PersonTestData" dataTableName="PersonData" dataAccessMethod="Sequential"/>
    </dataSources>
  </microsoft.visualstudio.testtools>
</configuration>

The Code that I'm using is this:

[TestMethod()]
[DeploymentItem("PersonTestData.xml")]
[DataSource("PersonTestData")]
public void CompareToTest()
{
    Person Test = (Person)TestContext.DataRow["Person"];
    Int32 result = Main.CompareTo(Test);
    Assert.IsNotNull(result);
}

And Finally the XML file It's self:

<?xml version="1.0" encoding="utf-8" ?>
<PersonData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Person>
    <LastName>Jones</LastName>
    <FirstName>Bill</FirstName>
    <Age>24</Age>
  </Person>
  <Person>
    <LastName>West</LastName>
    <FirstName>John</FirstName>
    <Age>24</Age>
  </Person>
  <Person>
   <LastName>Jones</LastName>
   <FirstName>Bill</FirstName>
   <Age>24</Age>
  </Person>
</PersonData>

Not sure where I'm going wrong at this point.

Was it helpful?

Solution

I think you don't need an ODBC connection string to read the xml file. Simply use the DataSource attribute as below. Also "PersonTestData.xml" properties. CopyToOutputDirectory set to "CopyAlways".

[TestClass]
public class UnitTest1
{
    private TestContext testContextInstance;
    public TestContext TestContext
    {
        get { return testContextInstance; }
        set { testContextInstance = value; }
    }

    [TestMethod]
    [DeploymentItem("PersonTestData.xml")]
    [DataSource("Microsoft.VisualStudio.TestTools.DataSource.XML",
               "|DataDirectory|\\PersonTestData.xml",
               "Person",
                DataAccessMethod.Sequential)]
    public void CompareToTest()
    {
        var row = TestContext.DataRow;
        var firstName = row["FirstName"].ToString();
        var lastName = row["LastName"].ToString();

        //Asserts...                 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top