我的单元测试数据源出现问题。我想用XML保存记录。据我所知,这是支持但我不断收到此错误“单元测试适配器无法连接到数据源......”。
我已按如下方式设置app.config:
<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>
我正在使用的代码是:
[TestMethod()]
[DeploymentItem("PersonTestData.xml")]
[DataSource("PersonTestData")]
public void CompareToTest()
{
Person Test = (Person)TestContext.DataRow["Person"];
Int32 result = Main.CompareTo(Test);
Assert.IsNotNull(result);
}
最后是XML文件它是自己的:
<?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>
目前还不确定我哪里出错了。
答案 0 :(得分:1)
我认为您不需要ODBC连接字符串来读取xml文件。只需使用DataSource属性,如下所示。还有“PersonTestData.xml”属性。 CopyToOutputDirectory设置为“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...
}
}