C# Unit Test with IO Hack

When you want to test a simple I/O function that won’t change parent directories and files, you can just add a file in the test project and set it to Always Copy to Output Directory

copy-always

Then you can use it as below:

[TestMethod]
public void ReadTest()
{
    Trace.WriteLine(File.ReadAllText("test.txt"));
}

However, this is only a hack, not the correct solution.

For the correct solution, see How do you mock out the file system in C# for unit testing?.

Posted:

Comments