中的单元测试的数据源

中的单元测试的数据源

本文介绍了如何使用多个Excel文件作为C#中的单元测试的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试进行一个单元测试,该单元测试具有来自多个excel文件的数据源,每个excel文件都包含一个用于同一单元测试的测试用例.我想将所有excel文件放在一个文件夹中,并让单元测试程序遍历所有excel文件.

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.

我发现了几种方法,例如将所有测试用例存储在XML文件中,但是这种方法太麻烦了,因为我必须从excel文件中提取所有测试用例并将其放入相同的XML文件中.我希望获得一种有效的方法.有什么建议吗?

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?

推荐答案

使用Visual Studio的标准测试框架(MsTest),您可以使用测试中的[DataSource]属性绑定到数据源.默认情况下,这包括使用Excel的OleDB驱动程序对Excel的支持.

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.

但是,这假设所有测试用例都位于同一excel文件中(因为您将使用一个简单的连接字符串来打开文件和选项卡).如果要从多个文件中获取案例,则需要通过实现名称空间System.Data中的rel ="nofollow noreferrer">自2003年以来,其背后的技术并未发生太大变化..

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.

如果您能够将excel文件合并为一个文件,并且每个测试用例都位于各自的行中,则能够使用标准的DataSource属性将数据输入到您的测试中.

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始终假定您的数据是以表格格式进行格式化的.

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

这篇关于如何使用多个Excel文件作为C#中的单元测试的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 02:11