There is some documentation to explain this already, but below is a step-by-step that shows how to use an Excel spreadsheet as a Data Source for both unit and web tests.
First, let’s set the stage. I’m going to use a solution containing a class library and a web site.
The class library has a single class with a single method that simply returns a “hello”-type greeting.
namespace SimpleLibrary { public class Class1 { public string GetGreeting(string name) { return "Hello, " + name; } } }
For my VB friends out there:
Namespace SimpleLibrary Public Class Class1 Public Function GetGreeting(ByVal name As String) As String Return "Hello, " & name End Function End Class End Namespace
Unit Testing
So now I’m going to create a unit test to exercise the “GetGreeting” method. (As always, tests go into a Test project. I’m calling mine “TestStuff”.)
Here’s my straightforward unit test:
[TestMethod()] public void GetGreetingTest() { Class1 target = new Class1(); string name = "Steve"; string expected = "Hello, " + name; string actual; actual = target.GetGreeting(name); Assert.AreEqual(expected, actual);
}
In VB:
<TestMethod()> _ Public Sub GetGreetingTest() Dim target As Class1 = New Class1 Dim name As String = "Steve" Dim expected As String = "Hello, " & name Dim actual As String actual = target.GetGreeting(name) Assert.AreEqual(expected, actual) End Sub
I’ll run it once to make sure it builds, runs, and passes:
I have an Excel file with the following content in Sheet1:
Nothing fancy, but I reserve the right to over-simplify for demo purposes. :)
To create a data-driven unit test that uses this Excel spreadsheet, I basically follow the steps you’d find on MSDN, with the main difference being in how I wire up my data source.
I click on the ellipsis in the Data Connection String property for my unit test.
Follow these steps to set up the Excel spreadsheet as a test data source for a unit test.
[DataSource("System.Data.Odbc", "Dsn=Excel Files;
dbq=|DataDirectory|\\ExcelTestData.xlsx;defaultdir=C:\\TestData;
driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$",
DataAccessMethod.Sequential),
DeploymentItem("TestStuff\\ExcelTestData.xlsx"), TestMethod()] public void GetGreetingTest() { Class1 target = new Class1(); string name = TestContext.DataRow["FirstName"].ToString(); string expected = "Hello, " + name; string actual; actual = target.GetGreeting(name); Assert.AreEqual(expected, actual); }
Again, in VB: <DataSource("System.Data.Odbc", "Dsn=Excel Files; dbq=|DataDirectory|\ExcelTestData.xlsx;defaultdir=C:\TestData; driverid=1046;maxbuffersize=2048;pagetimeout=5", "Sheet1$", DataAccessMethod.Sequential)> <DeploymentItem("TestStuff\ExcelTestData.xlsx")> <TestMethod()> _ Public Sub GetGreetingTest() Dim target As Class1 = New Class1 Dim name As String = TestContext.DataRow("FirstName").ToString() Dim expected As String = "Hello, " + name Dim actual As String actual = target.GetGreeting(name) Assert.AreEqual(expected, actual) End Sub
Again, in VB:
<DataSource("System.Data.Odbc", "Dsn=Excel Files;
dbq=|DataDirectory|\ExcelTestData.xlsx;defaultdir=C:\TestData;
DataAccessMethod.Sequential)>
<DeploymentItem("TestStuff\ExcelTestData.xlsx")> <TestMethod()> _ Public Sub GetGreetingTest() Dim target As Class1 = New Class1 Dim name As String = TestContext.DataRow("FirstName").ToString() Dim expected As String = "Hello, " + name Dim actual As String actual = target.GetGreeting(name) Assert.AreEqual(expected, actual) End Sub
Yippee!
Web Testing
You can achieve the same thing with a web test. So I’m going to first create a simple web test that records me navigating to the website (at Default.aspx), entering a name in the text box, clicking, submit, and seeing the results. After recording, it looks like this.
See “TxtName=Steve”? The value is what I want to wire up to my Excel spreadsheet. To do that:
That’s it in a simple nutshell! There are other considerations to keep in mind such as concurrent access, additional deployment items, and perhaps using system DSNs, but this should get you started.
Hi,
I would like to use data from a column in a excel file for eg. populate a list from a column. How would I go about it?
Thanks,
Saritha.
Saritha - in what context? Can you provide more details (or a scenario) of what you're trying to accomplish?