Including resources file for Unit test in C# project

My usual solution for this problem is that I refactor my program to open the file in the calling method and then pass a Stream instead of passing the filename and opening the file there.

For testing, this allows me to pass a MemoryStream so I can write my unit test without using the file system at all. It's sometimes even easier to check if the data has been written correctly and it's definitely faster, especially for a higher number of tests. You just have to remember to flush the MemoryStream after writing as .NET doesn't always do this automatically.

Example from one of my programs:

public TestSaveAndLoad()
{
  [... create data to save ...]
  using (MemoryStream targetStream = new MemoryStream())
  {
    target.Save(targetStream);
    targetStream.Flush();
    targetStream.Seek(0, ...);
    target.Load(targetStream);
  }
  [... assert that the loaded data equals the saved data ...]
}

An embedded resource doesn't exist on the file system, so it doesn't have any file path.

You have two options:

  • Change the API of your SUT so that it accepts a Stream instead of only a file path. This solution is much preferred.
  • Save the embedded resource to a temporary file during unit testing, making sure to delete it again after each test case.

The first solution is an excellent example of how TDD drives us towards better, more flexible APIs.