【问题标题】:Excel 2007 DataSource connection error in Visual Studio 2008 unit testVisual Studio 2008 单元测试中的 Excel 2007 DataSource 连接错误
【发布时间】:2009-07-07 18:19:21
【问题描述】:

我正在尝试在我的 VS 2008 测试项目中使用 Excel 2007 作为数据源进行单元测试。

配置文件:

<configuration>
    <configSections>
        <section name="microsoft.visualstudio.testtools"
                 type="Microsoft.VisualStudio.TestTools.UnitTesting.TestConfigurationSection, Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </configSections>
    <connectionStrings>
        <add name="MyExcelConn"
             connectionString="Dsn=Excel Files;dbq=SearchTestValues.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5"
             providerName="System.Data.Odbc" />
    </connectionStrings>
    <microsoft.visualstudio.testtools>
        <dataSources>
            <add name="MyExcelDataSource"
                 connectionString="MyExcelConn"
                 dataTableName="Sheet1$"
                 dataAccessMethod="Sequential"/>
        </dataSources>
    </microsoft.visualstudio.testtools>
</configuration>

我的测试代码:

[TestMethod]
[DeploymentItem("SearchTestValues.xlsx")]
[DataSource("MyExcelDataSource")]
public void ShouldReturnResultsValidity()
{
  var minDate = (DateTime)TestContext.DataRow["MinDate"];
  var maxDate = (DateTime)TestContext.DataRow["MaxDate"];
  var minStatus = (int)TestContext.DataRow["MinStatus"];
  var maxStatus = (int)TestContext.DataRow["MaxStatus"];

  var criteria = new SearchCriteria(minDate, maxDate, minStatus, maxStatus);

  Assert.IsTrue(criteria.IsValid());
}

excel 文件位于项目的根文件夹中。文件 BuildAction 设置为 Content 并且 Deploy 设置为如果较新则复制。我还将文件添加到部署项列表中,以便在运行测试时将 excel 文件部署到 TestResults 文件夹的 out 文件夹中。

当我运行此测试时,出现以下错误:

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: ERROR [42S02] [Microsoft][ODBC Excel Driver] The Microsoft Office Access database engine could not find the object 'Sheet1$'.  Make sure the object exists and that you spell its name and the path name correctly.

知道为什么我会收到此错误吗?

谢谢

【问题讨论】:

    标签: c# visual-studio-2008 unit-testing


    【解决方案1】:

    要解决此问题,您必须更改 Excel 文件的属性。

    构建操作:内容 复制到输出目录:如果较新则复制

    那么它应该可以工作了!

    【讨论】:

      【解决方案2】:

      试试

      dataTableName="[Sheet1$]"
      

      【讨论】:

      • 没用。这次的错误是:单元测试适配器连接数据源或读取数据失败。有关解决此错误的详细信息,请参阅 MSDN 库中的“数据驱动单元测试故障排除”(go.microsoft.com/fwlink/?LinkId=62412)。错误详细信息:错误 [42000] [Microsoft][ODBC Excel 驱动程序] 名称“[Sheet1$”的括号无效。
      • 只是为了确保,我修改了 dataTableName 与 "[Sheet1$]" 完全相同,但错误消息似乎错过了右括号。然而,它不起作用。
      【解决方案3】:

      我们遇到了同样的问题。我们尝试更改驱动程序、配置等。

      当然,将 Excel 电子表格另存为 97-2003 兼容(一个 .xls 而不是 .xlsx)可以解决问题。不确定这在您的情况下是否可行。

      【讨论】:

        【解决方案4】:

        我在使用 mstest.exe 部署测试时遇到了同样的问题 - 在目标机器上安装 VS 2008 为我解决了这个问题。 . .

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2010-10-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多