Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i reference an embedded data file for data-driven unit tests?

This is my scenario, I want to make a Data-Driven unit test, and for being environment-independent i want to embed the data file in my assembly. How can i reference then in the DataSourceAttribute of the Unit Test?.

I'm trying to access an Excel 2003 file using the Microsoft Jet OleDB Provider. The unit test works fine when the file is in the Hard-Drive but not when i try to access the embedded assembly file. Here is my example:

Original

[DeploymentItem("IHRM.Infrastructure.EFRepositories.Tests\DataDriven.xls"), DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\DataDriven.xls;Extended Properties=\"Excel 8.0\"", "Sheet1$", DataAccessMethod.Sequential), TestMethod]

Suggested

[DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="what to put here";Extended Properties=\"Excel 8.0\"", "Sheet1$", DataAccessMethod.Sequential), DeploymentItem("IHRM.Infrastructure.EFRepositories.Tests\DataDriven.xls"), TestMethod]

like image 536
Javier Avatar asked Nov 05 '22 18:11

Javier


1 Answers

I doubt the JET OLEDB has out of the box support for connecting to databases based on an embedded resources. I see two options to work around this:

1. extract the resource

In your test's ClassInitialize, you could extract the assembly resource to a known location. Refer to this known location in your JET connection string. Clean up the extracted file again in your test's ClassCleanup.

2. write your own data source

You might be able to write your own DbProviderFactory which supports accessing embedded excel files. To make such a factory discoverable for mstest, you'll have to register your factory with DbProviderFactories.

To see how mstest handles data sources internally, open up the Microsoft.VisualStudio.QualityTools.Common.dll assembly with reflector and start following the trail by dissassembling Microsoft.VisualStudio.TestTools.Utility.TestDataConnection.Create.

like image 136
Wim Coenen Avatar answered Nov 14 '22 01:11

Wim Coenen