Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit testing with an input file

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.

like image 263
JMarsch Avatar asked Oct 14 '09 16:10

JMarsch


1 Answers

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.

like image 131
Todd Stout Avatar answered Oct 12 '22 22:10

Todd Stout