有没有办法在目录中使用文件名作为测试的数据源? [英] Is there a way to use file names in a directory as the datasource for a test?

查看:45
本文介绍了有没有办法在目录中使用文件名作为测试的数据源?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单元测试来解析文件中的数据。  我希望在给定目录中的每个文件上运行测试。  我意识到我可以创建一个CSV并将路径放在该文件中,但我希望能够实时获取这些文件。

I have a unit test that parses data from files.  I would like the test to be run on each file in a given directory.  I realize I could create a CSV and put the paths in that file, but I would like for this to grab the files in real-time.

我查找了有关实现自定义数据源提供程序但无法找到任何内容的信息。

I have looked for information on implementing a custom datasource provider but cannot find anything.

我也尝试使用ClassInitialize方法创建文件,但似乎在初始化类之前评估测试方法属性(包括数据源文件),因此它表示该文件不存在。

I also tried using the ClassInitialize method to create the file, but it seems that the test method attributes (including the datasource file) are evaluated before the class is initialized so it says the file does not exist.

还有其他选择吗?

推荐答案

嗨Sam,

那里每个测试方法只能指定一个DataSource,这也意味着它基于文件夹或多个文件以外的文件(.csv,.xlsx等)。

There is only one DataSource could be specified to each test method, which also means it's based on a file(.csv, .xlsx etc) other than a folder or multiple files.

>> 使用ClassInitialize方法创建文件,但似乎在初始化类之前评估测试方法属性(包括数据源文件),因此它表示该文件不存在。

是的,它应该是这样的,在classinitialize之前调用datacource属性,你可以通过在ClassInitialize方法中放置一个断点来验证它。

Yes, it should be like that, the datacource attribute is called before the classinitialize you could verify it by putting a breakpoint at ClassInitialize method.

一个选项是通过C#代码读取csv文件:

An option is to read csv file by C# code:

http://stackoverflow.com/questions/3507498/reading-csv-files-using-c-好极了

祝你好运,

Fletch

 


这篇关于有没有办法在目录中使用文件名作为测试的数据源?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆