0

我们的测试方法通过 DataSource 属性连接到 Excel 工作簿。看起来像:

[TestMethod, Priority(3)]
[DataSource("System.Data.OleDB",
            "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\\TestDataWorkbook.xlsx; Extended Properties='Excel 12.0;HDR=yes';",
            "TestDataSheet$",
            DataAccessMethod.Sequential)]

这完美无缺。但是当我尝试在 App.Config 中添加相同的连接字符串时

<configuration>
  <configSections>
    <section name="microsoft.visualstudio.qualitytools" type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
  </configSections>
  <connectionStrings>
    <add name="TestDataConnectionString" connectionString="Provider=Microsoft.ACE.OLEDB.12.0; Data Source=|DataDirectory|\\TestDataWorkbook.xlsx; Extended Properties='Excel 12.0;HDR=yes';" providerName="System.Data.OleDb" />
  </connectionStrings>
  <microsoft.visualstudio.qualitytools>
    <dataSources>
      <add name="MyTestData" connectionString="TestDataConnectionString" dataTableName="TestDataSheet$" dataAccessMethod="Sequential"/>
    </dataSources>
  </microsoft.visualstudio.qualitytools>
... other configuration settings

并尝试通过以下方式连接它:

[TestMethod, Priority(3)]
[DataSource("MyTestData")]

它导致以下错误:

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: The Microsoft Office Access database engine could not find the object 'MyTestData$'.  Make sure the object exists and that you spell its name and the path name correctly.

错误存在于工作表名称中。我有一个到工作簿的连接(通过重命名工作簿和连接字符串来检查它),但是工作表名没有像第一个示例中那样被识别为表名。

有人认识到这种情况还是有人暗示了正确的方向?

4

1 回答 1

0

只需将此代码粘贴到您的测试方法之上:

[TestMethod, Priority(3)]
[DataSource("MyTestData"), TestMethod]

这将解决您的问题:)

于 2015-02-19T11:14:25.347 回答