数据驱动的单元测试问题

2024-02-18

我在将单元测试设置为使用 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 上的演练进行设置:演练:使用配置文件定义数据源 http://msdn.microsoft.com/en-us/library/ms243192.aspx。请注意,我确实更改了section版本为10.0.0.0因为我正在使用 .net 4.0(根据页面底部的注释)。

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


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

文件是只读的吗?

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

数据驱动的单元测试问题 的相关文章

随机推荐