2009-07-07 22 views
1

我想在我的VS 2008測試項目中使用Excel 2007作爲數據源進行單元測試。Visual Studio 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設置爲內容並且部署設置爲複製,如果更新。我還將該文件添加到部署項目列表中,以便在測試運行時將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. 

任何想法,爲什麼我收到此錯誤?

感謝

回答

0

嘗試

dataTableName="[Sheet1$]" 
+0

沒有工作。這次的錯誤是: 單元測試適配器無法連接到數據源或讀取數據。有關解決此錯誤的更多信息,請參閱MSDN Library中的「數據驅動的單元測試疑難解答」(http://go.microsoft.com/fwlink/?LinkId=62412)。 錯誤詳細信息:錯誤[42000] [Microsoft] [ODBC Excel驅動程序]名稱'[Sheet1 $'的包圍無效。 – 2009-07-08 13:28:41

0

我們有同樣的問題。我們嘗試更改驅動程序,配置等。

確實將Excel電子表格保存爲97-2003兼容(.xls而不是.xlsx)可解決此問題。不確定這是否可行。

2

要解決此問題,您必須更改Excel文件的屬性。

生成操作:內容 複製到輸出目錄:複製如果新

那麼它應該工作!

0

我在使用mstest.exe部署測試時遇到了同樣的問題 - 在目標計算機上安裝VS 2008爲我解決了這個問題。 。 。

相關問題