2

I am trying to make a unit test that have data source from multiple excel files, each excel file contains a test case for the same unit test. I would like to put all the excel files in one folder and let the unit test program to iterate through all the excel files.

I have found several methods, like storing all the test cases in a XML file but this method is too tedious as I have to extract all the test cases from the excel files and put it into the same XML file. I hope to get an efficient way to do it. Any suggestions?

jessehouwing
  • 106,458
  • 22
  • 256
  • 341
Mary June
  • 139
  • 4
  • 19
  • What have you tried so far? And which testing framework are you using? XUnit, NUnit, MsTest? And, which version of Visual Studio, if premium that gives some extra options. – jessehouwing Mar 30 '15 at 10:02
  • If MsTest, the most elegant way to integrate into the framework is to implement a DataProvider which you can link to a test case using the DataSource attribute. Implementing a provider is similar to building a custom ADO.NET provider and requires the implementation of a couple of interfaces. See: https://msdn.microsoft.com/en-us/library/microsoft.visualstudio.testtools.unittesting.datasourceattribute.providerinvariantname.aspx – jessehouwing Mar 30 '15 at 10:06
  • I am using MS Visual Studio Professional 2013. I am using Microsoft.VisualStudio.TestTools.UnitTesting namespace for the testing. – Mary June Mar 30 '15 at 10:07
  • With `xUnit` you'd use a `Theory` – DrKoch Mar 30 '15 at 10:11

1 Answers1

2

With the standard testing framework of Visual Studio (MsTest) you're able to bind to datasources using the [DataSource] attribute on your test. By default this includes support for Excel, using the OleDB driver for Excel.

However, this assumes that all your test cases live in the same excel file (since you'd use a simple connection string that opens a file and tab). If you want to grab the cases from multiple files, you'll need to implement your own Data Provider by implementing the namespaces from System.Data the technology behind this hasn't changed much since 2003.

If you're able to consolidate your excel files to a single file with your test cases each on their own row, you'd be able to use the standard DataSource attribute to feed the data into your test.

MsTest always assumes that your data is formatted in a tabular format.

Community
  • 1
  • 1
jessehouwing
  • 106,458
  • 22
  • 256
  • 341