I have a unit test that is currently set up to take inputs from a csv file defined using the DataSource attribute on the test. I'd like to change this so that instead of condensing several files into a csv, the unit test would run for each file in a directory. Is there a way to create the datasource dynamically so that it contains the list of filenames in the directory?
Asked
Active
Viewed 262 times
0
-
Take a look at the [Directory](https://msdn.microsoft.com/en-us/library/system.io.directory_methods(v=vs.110).aspx) class which has several methods for finding files and directories. – khargoosh Mar 16 '17 at 01:08
-
Yeah, I know how to get the directories and files. My question is, once I get those, how do I set the test datasource to the list of files? – Sam Mar 16 '17 at 02:01
-
Possible duplicate of [How to use multiple excel files as data source for unit test in C#?](http://stackoverflow.com/questions/29343246/how-to-use-multiple-excel-files-as-data-source-for-unit-test-in-c) – khargoosh Mar 16 '17 at 03:11
-
Another option is to write a small program that will locate then concatenate your multiple CSV files, and write them to a single CSV file to which your DataSource points. You could run this as a post build event. – khargoosh Mar 16 '17 at 03:12
-
Perhaps I didn't explain it well. The entire point of my question is to avoid condensing all of these files into a CSV. I want to provide a path and have the test execute on each file in that path. I realize the easy way around this is to put all the paths into a file, but I'd rather now have that file if possible. – Sam Mar 16 '17 at 17:34
1 Answers
0
There was essentially no way to do what I was originally asking. So instead, I created the test like normal, specifying a CSV/TXT file to be used as input for the test method. However, I added a pre-build event to the test project that ran a PowerShell script which created the input file with the paths for all the files in the target directory. This worked flawlessly.

Sam
- 1,325
- 1
- 13
- 26