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

发布于 2024-08-15 15:47:29 字数 775 浏览 2 评论 0原文

这是我的场景,我想进行数据驱动的单元测试,并且为了独立于环境,我想将数据文件嵌入到我的程序集中。我如何在单元测试的 DataSourceAttribute 中引用?

我正在尝试使用 Microsoft Jet OleDB Provider 访问 Excel 2003 文件。当文件位于硬盘驱动器中时,单元测试工作正常,但当我尝试访问嵌入的程序集文件时,单元测试工作正常。这是我的示例:

原始

[DeploymentItem("IHRM.Infrastruct.EFRepositories.Tests\DataDriven.xls"), DataSource("System.Data.OleDb", "Provider=Microsoft.Jet.OLEDB. 4.0;数据源=|DataDirectory|\DataDriven.xls;扩展属性=\"Excel 8.0\""、"Sheet1$"、DataAccessMethod.Sequential)、TestMethod]

建议

[DataSource("System. Data.OleDb", "Provider=Microsoft.Jet.OLEDB.4.0;数据源="此处放置的内容";扩展属性=\"Excel 8.0\"", "Sheet1$", DataAccessMethod .Sequential), DeploymentItem("IHRM.Infrastruct.EFRepositories.Tests\DataDriven.xls"), TestMethod]

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

抚笙 2024-08-22 15:47:29

我怀疑 JET OLEDB 是否具有开箱即用的支持连接到基于嵌入式资源的数据库。我看到有两个选项可以解决这个问题:

1。提取资源

在测试的 ClassInitialize,您可以提取程序集资源到一个已知位置。请参阅 JET 连接字符串中的这个已知位置。在测试的 类清理

2.编写您自己的数据源

也许能够编写自己的DbProviderFactory 支持访问嵌入的 Excel 文件。要使 mstest 可以发现这样的工厂,您必须 使用 DbProviderFactories

要了解 mstest 如何在内部处理数据源,请使用 reflector 并通过反汇编 Microsoft.VisualStudio.TestTools.Utility.TestDataConnection.Create 开始跟踪线索。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文