All: I have a unit test that is testing functionality that requires an input file. This test was built using VS 2008's built-in unit testing feature.
My problem is that the file needs to be discoverable by the unit test. However, when the test runs, it runs from a temporary "output" directory under the test results folder. It can't find my input file.
I have added the file to the unit test project, with a compile action of "none", and a copy to output directory option of "copy if newer", but the copy occurs to the normal VS output directory (under bin), and not to the unit test execution directory, so the file is not found. I don't want to hardcode paths to the file, as the test should run for anyone who checks out the unit test. I could put the input file in a solution folder, and let the test code "discover" the file by hardcoding a relative path back up the tree, but I figured that this had to be a common issue, so I wanted to check whether I was missing something.
Add the file as a resource to your test assembly. Then you can load it at runtime via Assembly.GetManifestResourceStream in your test setup.
Here's a convenient method I use to load resources:
public static class ResLoader
{
public static string AsString<T>(string resName)
{
using (var reader = new StreamReader(Assembly.GetAssembly(typeof(T))
.GetManifestResourceStream(resName)))
{
return reader.ReadToEnd();
}
}
}
T is any class contained in your test assembly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With