【问题标题】:Data driven unit tests problem数据驱动的单元测试问题
【发布时间】:2011-11-24 19:31:29
【问题描述】:

我在设置单元测试以使用 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 上的这个演练进行设置:Walkthrough: Using a Configuration File to Define a Data Source。请注意,我确实将section 版本更改为10.0.0.0,因为我使用的是.net 4.0(根据页面底部的注释)。

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

【问题讨论】:

  • 这是我更喜欢 NUnit 和 xUnit 而不是 MSTest 的原因之一。在代码中工作,使用 [TestCase] 或 [Theory] ​​更加方便。

标签: c# visual-studio-2010 unit-testing data-driven-tests


【解决方案1】:

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

文件是只读的吗?

【讨论】:

  • 啊,谢谢。使用完整路径有效。但是,我真的很想使用相对路径,因为该项目位于 subversion repo 中。你知道搜索的默认目录是什么吗?
  • 我的猜测是测试用完了 bin 目录。假设\bin\debug,也许尝试..\..TestData.xlsx的属性
  • 在控制台中写出来或快速归档。 DirectoryInfo ApplicationRoot = new DirectoryInfo(Environment.CurrentDirectory); 在我的测试项目中,我使用DirectoryInfo ApplicationRoot = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent; 来查找项目根目录。
  • 好的,所以通过添加[DeploymentItem],它将文件移动到正确的执行目录。我终于发现defaultdir=.; 必须是defaultdir=.\;(不知道为什么最后需要额外的斜线,但它确实使它起作用了)。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    你使用配置后;

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-05
      • 1970-01-01
      • 2016-11-26
      • 1970-01-01
      相关资源
      最近更新 更多