views:

103

answers:

2

I've got lots of unit tests which needs lots of txt, data, html etc. files. Externally storing these files makes life so much easier to update test cases adding new test cases etc.

However having dependencies in Unit Tests brings lots of headache in different systems and in different test runners.

What are the best practices?

  1. Externally storing them and relatively linking these files in the code? (causing problems in some test runners, or requires extra configuration)
  2. Embedding all these files in the Unit Test dlls and read from there (makes creating tests harder)
  3. Storing in a hardcoded location(obviously causing so many problems when you check out the code in a different place)

How do you solve this problem?

+1  A: 

My practice has been to embed test resources in unit-test assemblies and pull them out using GetManifestResourceStream.

NUnit testing is fixture-oriented anyway, so once you have the fixture (i.e., a particular set of resources) set up, adding additional tests is easy.

Greg Bacon
+3  A: 

I use a local folder in my test project and get the test files with code like:

public static FileInfo GetTestFileInfo(string fileName)
{
    var dir = AppDomain.CurrentDomain.BaseDirectory;
    return new FileInfo(dir + @"\..\..\TestData\" + fileName);
}

Oh yes, I'm using MbUnit.

Hans Malherbe
AFAIK MBUnit support working directory options. I think nUnit is doing something awkward although I just noticed if I leave it with "TestData\xxx.txt" it works. At least for now. I was trying to get current directory.
dr. evil