Data-driven unit tests are a great way to run lots of tests. You define a test harness (or method) and tie it to a spreadsheet in an Excel file to “drive” the test – each row in the spreadsheet is one iteration of the test case. I am not going to go into any more detail here – there are plenty of examples about how to set these up. Another advantage of using Excel for data-driven tests is that testers love Excel. They can open and edit the spreadsheet and add / edit / remove test cases at will.
When a developer creates a test method, they can specify the spreadsheet to connect to using the [DataSource] attribute. Usually, you’d have a spreadsheet in your solution and set it to deploy in the test settings file (or by marking its properties to copy to output on a build) and then use the |DataDirectory| placeholder in the [DataSource] attribute to tell the test to look in the build output folder for the Excel file to open.
There are some drawbacks to doing this if your test team is maintaining the spreadsheets. They’ll need to have access to the source folder (out of Visual Studio) in order to change the spreadsheet. This is hardly ideal. A much better approach would be to set up a document library on a fileshare. Going one step better, set up a document library on Sharepoint – that way you get version history on the test data spreadsheets.
So if you decide to put the spreadsheets in a Sharepoint document library, you’ll have to decide on which site you want to store your docs on and then jump through a few hoops to hook up the tests to the Sharepoint documents. It makes sense to use the Team Project Portal for these sorts of documents, but any Sharepoint site will do.
Once you’ve uploaded your Excel files to a document library (don’t forget to enable Versioning if you want to track changes to these files), you have to do 2 things to get the test methods to work.
Firstly, you’ll have to map a network drive to the document library in the [ClassInitialize] method of your test class. This maps the document library of sharepoint to a local drive letter. You need to do this programmatically since the mapping won’t persist after you log out. Make sure that the account that is running the build controller has at least read access to the document library! I downloaded (and tweaked) this project from CodeProject to get some code for mapping network drives. I then created a method that map the drive:
public static void MapNetworkDrive(string localDrive, string serverDrive)
var drive = new NetworkDrive();
drive.PromptForCredentials = false;
drive.ShareName = serverDrive;
drive.LocalDrive = localDrive;
drive.Force = true;
I’m telling the library no to prompt for credentials and setting Force to true will unmap if the mapping exists already. You can then call this method from the [ClassInitialize] method:
public static void Setup(TestContext context)
Here I am mapping the z: to the root of the test document library on the Sharepoint portal for my Project. Once this is done, the [DataSource] attribute can just reference any spreadsheet from within this folder:
[Description("Testing database component to add error information")]
[DataSource("System.Data.Odbc", @"Dsn=Excel Files;dbq=z:\Error\ULP_Error_ErrorManager_Add.xlsx;defaultdir=.;driverid=1046;maxbuffersize=2048;pagetimeout=5", "ErrorAdd$", DataAccessMethod.Sequential)]
public void AddErrorTest()
There’s one other gotcha that you might see – the test run on the build fails saying that the data source could not be opened. In this case, log into your build server as your build service (tfsbuild or whatever identity you’re using) and open any Excel file – make sure that you are not being prompted for your name and initials (a popup that appears the very first time you use Excel). This will break the build until you’ve dismissed this dialogue once.
A last aside: when trying to set up the document library for the spreadsheets, we could not get the Explorer View working in Sharepoint – we just got a “Web page could not be opened”. After using Fiddler, I could see an HTTP 405 error. Googling a bit, I found that in order for Sharepoint’s own WebDAV to work (that’s what the Sharepoint Explorer View uses), you need to uninstall IIS’s WebDAV feature! Go figure…