【问题标题】:DataSource connection as attribute works, while it results in an error when defined in App.ConfigDataSource 连接作为属性有效,但在 App.Config 中定义时会导致错误
【发布时间】:2013-06-14 18:49:39
【问题描述】:

我们的测试方法通过 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.

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

是否有人认识到这种情况或有人有正确方向的提示?

【问题讨论】:

  • 你有没有想过这个问题?
  • @Matt 不,但我发现:我现在使用三个consts 作为数据源参数。这些必须const而不是readonly string

标签: c# unit-testing datasource app-config


【解决方案1】:

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

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

这会解决你的问题:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-22
    • 2014-05-17
    • 2016-06-01
    • 2015-10-28
    相关资源
    最近更新 更多