Junit tests: same package, different directory

Sunday, 2 May 2004

Put TestCase classes in the same package as the class they are testing. This makes it easy to test protected and package-visible methods. It’s still a pain to test private methods though.

Put TestCase files in a separate directory to the main source tree. This stops the source tree becoming cluttered with test classes, and also makes it easy to exclude them from your product release builds (since they don’t belong there).

For example:

Class: com.zikzak.widget.WidgetBuilder
Defined in: src/com/zikzak/widget/WidgetBuilder.java

TestCase: com.zikzak.widget.WidgetBuilderTest
Defined in: test/com/zikzak/widget/WidgetBuilderTest.java

Check your tests in to your source control system, so other developers (and you!) can run your tests to help verify changes in the future.

Tags:

Leave a comment