如何为数据驱动的单元测试引用嵌入式数据文件?

时间:2022-07-05 06:13:59

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?.

这是我的场景,我想做一个数据驱动的单元测试,为了与环境无关,我想在程序集中嵌入数据文件。那么如何在单元测试的DataSourceAttribute中引用呢?

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:

我正在尝试使用Microsoft Jet OleDB提供程序访问Excel 2003文件。当文件在硬盘驱动器中,而当我试图访问嵌入式程序集文件时,单元测试可以正常工作。这是我的例子:

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]

[DeploymentItem(“IHRM.Infrastructure.EFRepositories.Tests \ DataDriven.xls”),数据源(“System.Data。数据来源=|DataDirectory|\DataDriven.xls;扩展属性=\“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]

(数据源(“System.Data。数据来源=“放在这里的东西”;扩展属性=“Excel 8.0\”、“Sheet1$”、“DataAccessMethod.Sequential”、“部署方法”

1 个解决方案

#1


2  

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:

我怀疑JET OLEDB是否支持基于嵌入式资源的数据库连接。我看到了两种解决方案:

1. extract the resource

1。提取的资源

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.

在您的测试的ClassInitialize中,您可以将程序集资源提取到一个已知的位置。在您的JET连接字符串中引用这个已知位置。在测试的ClassCleanup中再次清理提取的文件。

2. write your own data source

2。编写自己的数据源

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.

您可以编写自己的DbProviderFactory,它支持访问嵌入式excel文件。要使mstest能够发现这样的工厂,您必须向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.

要了解mstest如何在内部处理数据源,打开Microsoft.VisualStudio.QualityTools.Common。用反射器组装dll,并通过拆卸microsoft.visualstudio.testtools . utility . use . testdataconnection.create开始跟踪。

#1


2  

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:

我怀疑JET OLEDB是否支持基于嵌入式资源的数据库连接。我看到了两种解决方案:

1. extract the resource

1。提取的资源

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.

在您的测试的ClassInitialize中,您可以将程序集资源提取到一个已知的位置。在您的JET连接字符串中引用这个已知位置。在测试的ClassCleanup中再次清理提取的文件。

2. write your own data source

2。编写自己的数据源

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.

您可以编写自己的DbProviderFactory,它支持访问嵌入式excel文件。要使mstest能够发现这样的工厂,您必须向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.

要了解mstest如何在内部处理数据源,打开Microsoft.VisualStudio.QualityTools.Common。用反射器组装dll,并通过拆卸microsoft.visualstudio.testtools . utility . use . testdataconnection.create开始跟踪。