3

我在设置单元测试以使用 Excel .xlsx 数据源时遇到了一些麻烦。

我的 App.config 文件:

<?xml version="1.0" encoding="utf-8" ?>
<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="TestData" connectionString="Dsn=Excel Files;dbq=TestData.xlsx;defaultdir=.; driverid=790;maxbuffersize=2048;pagetimeout=5" providerName="System.Data.Odbc" />
  </connectionStrings>
  <microsoft.visualstudio.testtools>
    <dataSources>
      <add name="GetAllCellNamesTest" connectionString="TestData" dataTableName="GetAllCellNamesTest$" dataAccessMethod="Sequential"/>
</dataSources>

我已经验证它正在查找TestData.xlsx,并且有一张名为GetAllCellNamesTest.

在我的单元测试类中,我有以下设置:

        [TestMethod()]
        [DeploymentItem("TestProject\\TestData.xlsx")]
        [DataSource("GetAllCellNamesTest")]
        public void GetAllCellNamesTest()
        {
            // ... test code

TestData.xlsx正在被复制到测试结果目录,并且所有不尝试引用数据源的单元测试都通过了。

但是,此测试失败并显示以下消息:

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 Access database engine could not find the object 'GetAllCellNamesTest$'. Make sure the object exists and that you spell its name and the path name correctly. If 'GetAllCellNamesTest$' is not a local object, check your network connection or contact the server administrator.

我真的不确定我的设置哪里出错了,我按照 MSDN 上的这个演练进行设置:演练:使用配置文件定义数据源。请注意,我确实将section版本更改为,10.0.0.0因为我使用的是 .net 4.0(根据页面底部的注释)。

编辑:哦,所有文件都位于我的计算机本地。

4

3 回答 3

3

您是否尝试过使用完整的文件路径?

文件是只读的吗?

于 2011-09-25T21:40:39.580 回答
2

您还可以将要部署的文件/目录指定为 TestSettings 的一部分。这将节省您必须为每个测试方法放置 DeploymentItem 属性的工作。

于 2011-11-08T07:29:47.027 回答
1

之后使用配置;

<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>
于 2012-03-28T16:08:28.470 回答