单元测试 之 JUnit
JUnit循序渐进 --copy of《JUnit Cookbook》 from http://junit.sourceforge.net
竹笋炒肉 对该文和JUnit Best Practice有很好的翻译,可以参考他的Blog
JUnit Cookbook
Kent Beck, Erich Gamma
Here is a short cookbook showing you the steps you can follow in writing and organizing your own tests using JUnit.
Simple Test Case
How do you write testing code?The simplest way is as an expression in a debugger. You can change debug expressions without recompiling, and you can wait to decide what to write until you have seen the running objects. You can also write test expressions as statements which print to the standard output stream. Both styles of tests are limited because they require human judgment to analyze their results. Also, they don't compose nicely- you can only execute one debug expression at a time and a program with too many print statements causes the dreaded "Scroll Blindness".
JUnit tests do not require human judgment to interpret, and it is easy to run many of them at the same time. When you need to test something, here is what you do:
- Create an instance of TestCase:
- Create a constructor which accepts a String as a parameter and passes it to the superclass.
- Override the method runTest()
- When you want to check a value, call assertTrue() and pass a boolean that is true if the test succeeds
public void testSimpleAdd() {If you want to write a test similar to one you have already written, write a Fixture instead. When you want to run more than one test, create a Suite.
Money m12CHF= new Money(12, "CHF");
Money m14CHF= new Money(14, "CHF");
Money expected= new Money(26, "CHF");
Money result= m12CHF.add(m14CHF);
assertTrue(expected.equals(result));
}
Fixture
What if you have two or more tests that operate on the same or similar sets of objects?Tests need to run against the background of a known set of objects. This set of objects is called a test fixture. When you are writing tests you will often find that you spend more time writing the code to set up the fixture than you do in actually testing values.
To some extent, you can make writing the fixture code easier by paying careful attention to the constructors you write. However, a much bigger savings comes from sharing fixture code. Often, you will be able to use the same fixture for several different tests. Each case will send slightly different messages or parameters to the fixture and will check for different results.
When you have a common fixture, here is what you do:
- Create a subclass of TestCase
- Create a constructor which accepts a String as a parameter and passes it to the superclass.
- Add an instance variable for each part of the fixture
- Override setUp() to initialize the variables
- Override tearDown() to release any permanent resources you allocated in setUp
public class MoneyTest extends TestCase {Once you have the Fixture in place, you can write as many Test Cases as you'd like.
private Money f12CHF;
private Money f14CHF;
private Money f28USD;
protected void setUp() {
f12CHF= new Money(12, "CHF");
f14CHF= new Money(14, "CHF");
f28USD= new Money(28, "USD");
}
}
Test Case
How do you write and invoke an individual test case when you have a Fixture?Writing a test case without a fixture is simple- override runTest in an anonymous subclass of TestCase. You write test cases for a Fixture the same way, by making a subclass of TestCase for your set up code and then making anonymous subclasses for the individual test cases. However, after a few such tests you would notice that a large percentage of your lines of code are sacrificed to syntax.
JUnit provides a more concise way to write a test against a Fixture. Here is what you do:
- Write the test case method in the fixture class. Be sure to make it public, or it can't be invoked through reflection.
- Create an instance of the TestCase class and pass the name of the test case method to the constructor.
public void testMoneyMoneyBag() {Create an instance of of MoneyTest that will run this test case like this:
// [12 CHF] + [14 CHF] + [28 USD] == {[26 CHF][28 USD]}
Money bag[]= { f26CHF, f28USD };
MoneyBag expected= new MoneyBag(bag);
assertEquals(expected, f12CHF.add(f28USD.add(f14CHF)));
}
new MoneyTest("testMoneyMoneyBag")When the test is run, the name of the test is used to look up the method to run.
Once you have several tests, organize them into a Suite.
Suite
How do you run several tests at once?As soon as you have two tests, you'll want to run them together. You could run the tests one at a time yourself, but you would quickly grow tired of that. Instead, JUnit provides an object, TestSuite which runs any number of test cases together.
For example, to run a single test case, you execute:
TestResult result= (new MoneyTest("testMoneyMoneyBag")).run();To create a suite of two test cases and run them together, execute:
TestSuite suite= new TestSuite();Another way is to let JUnit extract a suite from a TestCase. To do so you pass the class of your TestCase to the
suite.addTest(new MoneyTest("testMoneyEquals"));
suite.addTest(new MoneyTest("testSimpleAdd"));
TestResult result= suite.run();
TestSuite constructor.
TestSuite suite= new TestSuite(MoneyTest.class);
TestResult result= suite.run();
Use the manual way when you want a suite to only contain a subset of the test cases. Otherwise the automatic suite extraction is the preferred way. It avoids you having to update the suite creation code when you add a new test case.
TestSuites don't only have to contain TestCases. They contain any object that implements the Test interface. For example, you can create a TestSuite in your code and I can create one in mine, and we can run them together by creating a TestSuite that contains both:
TestSuite suite= new TestSuite();
suite.addTest(Kent.suite());
suite.addTest(Erich.suite());
TestResult result= suite.run();
TestRunner
How do you run your tests and collect their results?Once you have a test suite, you'll want to run it. JUnit provides tools
to define the suite to be run and to display its results. You make your
suite accessible to a TestRunner tool with a static method suite
that returns a test suite.
For example, to make a MoneyTest suite available to a TestRunner, add
the following code to MoneyTest:
public static Test suite() {If a TestCase class doesn't define a suite method a TestRunner will extract a suite and fill it with all the methods starting with "test".
TestSuite suite= new TestSuite();
suite.addTest(new MoneyTest("testMoneyEquals"));
suite.addTest(new MoneyTest("testSimpleAdd"));
return suite;
}
JUnit provides both a graphical and a textual version of a TestRunner tool. Start it by typing java junit.awtui.TestRunner or junit.swingui.TestRunner. The graphical user interface presents a window with:
- a field to type in the name of a class with a suite method,
- a Run button to start the test,
- a progress indicator that turns from red to green in the case of a failed test,
- a list of failed tests.
In a dynamic programming environment like VisualAge for Java which supports hot code update you can leave the JUnit window up all the time. In other environments you have to restart the graphical version for each run. This is tedious and time consuming. As an alternative JUnit's AWT and Swing UIs use junit.runner.LoadingTestCollector . This LoadingTestCollector reloads all your classes for each test run.This feature can be disabled by unchecking the 'Reload classes every run' checkbox.
There is a batch interface to JUnit, also. To use it typejava junit.textui.TestRunner followed by the name of the class with a suite method at an operating system prompt. The batch interface shows the result as text output. An alternative way to invoke the batch interface is to define a main method in your TestCase class.
For example, to start the batch TestRunner for MoneyTest, write:
public static void main(String args[]) {With this definition of main you can run your tests by simply typing java MoneyTest at an operating system prompt.
junit.textui.TestRunner.run(suite());
}
For using either the graphical or the textual version make sure that
the junit.jar file is on your CLASSPATH.