I was wondering what the general best practice is for when you have multiple unit test fixtures testing different things that use the same file paths.

My test files are in a directory tree with certain rules so currently I am reusing directory macros in each file, i.e.

#define TYPE1FILE "Type1Files/"
#define TYPE2FILE "Type2Files/"

#define GENERIC_FILE "generic/"
#define SPECIAL_FILE "special/"

#define BAR1 TYPE1FILE GENERIC_FILE "/foo.txt"
#define BAR2 TYPE1FILE SPECIAL_FILE "/foo2.txt"
#define BAR3 TYPE2FILE GENERIC_FILE "/foo.txt"
#define BAR4 TYPE2FILE SPECIAL_FILE "/foo2.txt"

However, this is starting to cause duplication in multiple files because multiple test fixtures will use the same:

#define BAR1 TYPE1FILE GENERIC_FILE "/foo.txt"

if they both use the same file.

Also, it seems like it may be better to give them some scope by using variables inside the fixture or at least within the file.

I like being able to see a list of what files are being used for each test, but I am also considering moving the BAR defines all into a shared header file. I am not crazy about that idea because it will end up being a giant list of files without any indication of what is used where. Also it couples the test cases together more.

Does anyone have any suggestions?

  • What's most important here? Reuse of files and paths across tests, or just organizing them so they make sense?
    – Jennifer S
    Sep 8, 2011 at 18:59

2 Answers 2


Aren't you actually coupling your implementation to a directory structure on the filesystem?

That point aside, maybe the problem has gone beyond what you can satisfactorily accomplish with #define and it's time to write some code to handle it.

I'm thinking a simple string GetMyFilename(Conditions condition) so you can utilise all the power of your development language and not just the macro-preprocessor to handle what file you need for your tests.

My general point is that tests are first class citizens of the code base, don't be afraid to use all your skills and disciplines on them.

  • +1 My general point is that tests are first class citizens of the code base Oct 16, 2011 at 14:56

Well, this isn't rocket science, but I had an application where I needed to test 20 different file versions. I just gave each similar file a version specific prefix. Your tests will group together differently, but my suggestion is to add a test or test group specific file prefix.

  • Is he asking about versioning or about sharing test file between tests, and coming up with sensible file lists per test? Jul 27, 2011 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.