Friday, July 4, 2008

Setup Data-Driven Tests in Visual Studio to use Excel

I prefer simplicity of Excel files for my data-driven unit tests in Visual Studio - both 2005 and 2008. However the new test data source wizard is not supporting Excel files so the connection has to be setup manually.
It is not too hard to set it up, all that is required is addition of DataSourceAttribute to your test method:


[TestMethod]
[DeploymentItem("testData.xls")]
[DataSource(“System.Data.Odbc”,
@”Driver={Microsoft Excel Driver (*.xls)};DriverId=790;Dbq=testData.xls;DefaultDir=.”,
“Sheet1$”, DataAccessMethod.Sequential)]

public void SomeTest(){
//some testing
}




DeploymentItemAttribute is there so testData.xls file would be deployed with the test and thus make the test independent of a machine on which it is run. More about using Excel to drive your unit tests with some tips you can find here: Data-Driven Unit Testing using Excel in Visual Studio.

No comments:

Post a Comment