I’m relatively new to unit testing, and I’ve discovered that while many sources say that you should write unit tests, few of them give any indication where to put them in your project.
The only suggestions that I’ve seen are to put them alongside the production code or to build a directory structure that mirrors your production code. The problem I see with the first approach is that it would become difficult to extract those tests when you make a full build of the system; for the second, how would you test functionality that is only visible at the package level?
Is there a decent way to organize unit tests so that you can easily automate the build process and still access everything that needs to be tested?
EDIT: For those wondering about a specific language, I’m mostly working in Java. However, I’d like to find a solution that’s relatively language-agnostic so I can apply it regardless of what platform I’m working on.
You could always filter classes based on some naming convention like a special suffix or prefix when packaging (e.g. don’t include
**/*Test.classwith Java). I don’t really like this approach though, I find it messy and fragile.Unless I’m missing something, I don’t see the issue. You CAN have classes in the same package and have them living in different trees, e.g.:
src/main/javafor application sourcessrc/main/java/foo/Bar.javasrc/test/javafor tests sourcessrc/test/java/foo/BarTest.javaBoth
Bar.javaandBarTest.javaare in the samefoopackage but in different directories. This is the approach I use.Well, as hinted, my test sources and application sources live in the same project but in distinct directory trees and this works really well for me.
This is actually the default layout suggested by Maven (see the Introduction to the Standard Directory Layout). Whatever language you use, this might give you some ideas.