views:

137

answers:

1

How can I use an external data file in my Visual Studio unit tests? If I try to just include it in the test project and set Copy To Output Directory to true, it still can't be found.

What I have is:

[TestMethod]
public void DoMyTest() {
    using (StreamReader rdr = new StreamReader("MyTestData.txt")) {
        blahblah
    }
}

However, the file does not exist so I get an exception. My test data doesn't map well to XML or CSV, so using the DataSourceAttribute is not a viable option.

+4  A: 

Add your file as a resource, then call:

string myTestData = Project.Properties.Resources.MyTestData;

Unless the file is created dynamically by another process, this is how I work with all non-executable text files in Visual Studio. It's like falling off a log.

Chris McCall