我在设置单元测试以使用 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(根据页面底部的注释)。
编辑:哦,所有文件都位于我的计算机本地。