How to use multiple excel files as data source for unit test in C#?

蹲街弑〆低调 提交于 2019-12-24 05:27:31

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/29343246/how-to-use-multiple-excel-files-as-data-source-for-unit-test-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!