Unit testing with input file

All: I have a unit test that checks for functionality that requires an input file. This test was built using the built-in testing feature of VS 2008.

My problem is that the file must be accessible for detection using unit test. However, when the test is executed, it starts from a temporary “output” directory in the test results folder. It cannot find my input file.

I added the file to the unit test project with compilation "none" and a copy to display the "copy if newer" directory option, but the copy occurs in the usual VS output directory (in bin), and not in the unit test execution directory, so the file was not found. I do not want to hardcode the paths to the file, since the test should be run for everyone who checks the unit test. I could put the input file in the solution folder, and let the test code "detect" the file by hard-coding the relative path of the backup tree, but I decided that this should be a common problem, so I wanted to check that something was missing.

+5
source share
3 answers

. Assembly.GetManifestResourceStream .

, :

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 - , .

+4

, , . . , , , .

+3
source

All Articles