Guidelines for writing unit tests
From Mallet
When you add code to base/, please write unit tests if you can. The idea behind a unit test is (a) it usually tests a small part of your code, perhaps even a single function, and (b) the test needs to be completely automated, i.e., it can tell on its own whether it succeeded, without human intervention. We use JUnit (http://www.junit.org) for writing tests, which is fairly simple to use.
Short version: Most of the time, you can create unit tests simply by copying the class base/util/TestCaseTemplate.
Long version: We have a test harness that runs all our test cases automatically. In order to play nicely with this test harness, your test cases need to follow some guidelines, listed below. They seem like a lot, but once you know them, they're really quite easy.
- Your test case must be named Test_____ for some value of ______. No other classes should be named this way, except for test cases.
- Your test case must extend the JUnit test case class. See the class base/util/TestCaseTemplate for an example you can duplicate.
- Your test case must have a no argument constructor.
- Your test case may require files from disk (e.g., data files, or serialized Java objects from older versions of the code). If so, these files must be committed to cvs. You may commit them under the top-level test/ directory, which is a sibling of src/. For example, if your test case is
src/edu/umass/cs/mallet/base/util/tests/TestMyNewClass.java
then your data file can be committed to cvs at
test/edu/umass/cs/mallet/base/util/my-data-file.txt
- Where should your test case be located? Most of time, place your test case in a tests/ directory underneath the directory of the java files you're testing. For example, if your test case is for a class in
src/edu/umass/cs/mallet/base/util/
then most of the time put the test case in
src/edu/umass/cs/mallet/base/util/tests/
Sometimes this isn't feasbile, in particular when your test case needs package-local access to the class you're testing. You could put the test case in base/util, but this could clutter up the higher-level directory. Instead, you may place your test case in the test/ hierarchy as well, for example
test/edu/umass/cs/mallet/base/util/