JUnit is the original testing framework, since copied by every other major programming language.
In a sense, the purpose of JUnit testing is to reduce the number of bugs in the final code. Even more importantly, however, extensive use of JUnit, particularly in combination with Test Driven Development, results in substantially better code quality.
There are two versions of JUnit currently available: JUnit 3 and JUnit 4. Python's
unittest is slightly out of date, as it is based on JUnit 3. The most important difference is that JUnit 4 uses annotations (words beginning with
@). You should use JUnit 4 for your assignments.
|Your class can extend any other class you like (
|You can annotate a method with
and it will be executed before every test. The method can have any name (but it's commonly still named
You can create as many test methods as you like, but:
|You can specify that a test method should get an exception with the annotation
(or whatever kind of Exception you are expecting).
|You can set a limit on how many milliseconds a test method is allowed to take with the annotation
|To execute a JUnit method after each test method, annotate it with
(This is rarely used.)
There about 40 tests that you can use in your test methods; see http://junit.org/javadoc/latest/org/junit/Assert.html. Here are the ones that I have found to be the most useful.
actualvalue equals the
actualmust be the same type (for example, you cannot test an
double). If the two parameters are objects of a class that you have created, the test will use your
public boolean equals(Object o)method (or
==if you haven't defined
equals, which is almost certainly not what you want!).
assertEquals, but for arrays. If the elements of the arrays are objects of a class that you have created, the test will use the
equalsmethod for that class.
assertEquals(expected, actual, delta)
actualargument is another reference to the
actualargument is not another reference to the
the test, but it's certainly returning the wrong answer. The method
should compute and return a BalancedTernary.