我正在使用VSTS 2K8,並且已經設置了單元測試項目。在它裏面,我有一個測試類和一個簡單斷言的方法。我正在使用Excel 2007電子表格作爲我的數據源。單元測試錯誤 - 單元測試適配器無法連接到數據源或讀取數據
我的測試方法是這樣的:
[DataSource("System.Data.Odbc",
"Dsn=Excel Files;dbq=|DataDirectory|\\MyTestData.xlsx;defaultdir=C:\\TestData;driverid=1046;maxbuffersize=2048;pagetimeout=5",
"Sheet1",
DataAccessMethod.Sequential)]
[DeploymentItem("MyTestData.xlsx")]
[TestMethod()]
public void State_Value_Is_Set()
{
string expected = "MD";
string actual = TestContext.DataRow["State"] as string;
Assert.AreEqual(expected, actual);
}
正如方法裝飾屬性表明,我的Excel電子表格是我本地C:/驅動器。其中,我所有數據所在的工作表被命名爲「Sheet1」。
我已將Excel電子表格複製到我的項目中,並且已將其設置爲Build Action =「Content」,並且已將其複製到輸出目錄=「複製,如果是新的」。
當試圖運行這個簡單的單元測試,我收到以下錯誤:
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.
我驗證過表名稱拼寫是否正確(即工作表Sheet1)和我覈實,我的數據源設置正確。
網頁搜索一點也沒有發現。我完全被難住了。
所有的幫助或輸入是讚賞!!!!
工作表名稱應附加'$',否則它肯定會失敗(對於Visual Studio 2015仍然有效),例如: 「Sheet1 $」 – DeChrist 2017-03-23 15:38:18