Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I noticed some duplicate code in a codebase I am working on that appended a filename to a directory path, so I decided to refactor it into its own method. The application I am working on is not well tested, however; I just set up the first unit tests on it two days ago, so I am very concerned to get as much code under test as possible whenever I touch it.

So I began to write a unit test like this (pseudocode):

Directory directory("a")

However, I then remembered that the application is cross-platform. So I thought about this:

Directory directory("a")
String separator = "/" if (platform is *nix) else "\"
Expect(directory.prependPathToFilename("b")).toBeEqualTo("a" + separator + "b")

But I remembered that Roy Osherove says in section 7.1.2 ("Avoiding logic in tests") of The Art of Unit Testing (178):

If you have any of the following inside a test meethod, your test contains logic that should not be there:

  • switch, if, or else statements

  • foreach, for or while loops

A test that contains logic is usually testing more than one thing at a time, which isn't recommended, becauset he test is less readable and more fragile. But test logic also adds complexity that may contain a hidden bug.

Now, it does not seem that in this case I am testing more than one thing at a time. Would this be a case of an acceptible logic block in a test? Is there a pattern for cleanly testing behaviors/results that are expected to be different on different platforms?

share|improve this question
what language are you using? Surely there are library classes or functions for file path manipulation. –  kevin cline Mar 29 '13 at 18:55
@kevincline I am in an odd situation where I am using C++ but have to support some legacy compilers, and Boost is frowned upon. Also, I am trying to make small steps by refactoring within the current structure (i.e. I added the method to the Directory class which owns the path already). –  Kazark Mar 29 '13 at 18:59
add comment

2 Answers

up vote 10 down vote accepted

It is disputed how religious you have to be about unit testing - surely there is some value to tests even if they violate some of the precepts of the Wise Elders of testing (certainly more value than not having them!).

However, in this instance you need not break the rules if you don't want to. Note that your test is only testing one of the possible separators, because by definition it runs only on one platform. If you make the platform-detecting routing mockable, you can write different tests and inject different pseudo-platforms into them. Each of the tests then need not have any logic in it, and you achieve greater coverage at the same time.

share|improve this answer
add comment

To keep your unit test clean and simple, you could extend the test library by adding a new assertion for paths. This new assertion would abstract from the different path separators. Your test would then look like

Directory directory("a")

and - depending on implementation - work identically when using

Directory directory("a")
share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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