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.
这是一个简短的说明,你可以跟随用JUnit书写和组织你自己的测试
简单的测试用例
How do you write testing code?
如何编写测试代码?
最简单的方法是作为一个表达式在debugger中。你可以不用重新编译的去改变调试表达式,并且你能等待去决定直到你看到运行对象使书写
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:
For example, to test that the sum of two Moneys with the same currency contains a value which is the sum of the values of the two Moneys, write:例如,测试
@Test public void simpleAdd() { 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)); }
If you want to write a test similar to one you have already written, write a Fixture instead.
如果你想要写一个类似于您已经写好的测试时,写一个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:
For example, to write several test cases that want to work with different combinations of 12 Swiss Francs, 14 Swiss Francs, and 28 US Dollars, first create a fixture:
例如,写一些测试用例去和不同的合作12法国货币,14法国国币,和28美元,首先建立一个fixture:
public class MoneyTest { private Money f12CHF; private Money f14CHF; private Money f28USD; @Before public void setUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); f28USD= new Money(28, "USD"); } }
Once you have the Fixture in place, you can write as many Test Cases as you'd like. Add as many test methods (annotated with @Test) as you'd like.
一旦有了Fixture,你能写一些你喜欢的测试用例。加一些你喜欢测试方法(用@Test注释)
运行测试
How do you run your tests and collect their results?
怎么运行你的测试并关联这些结果?
Once you have tests, you'll want to run them. JUnit provides tools to define the suite to be run and to display its results. To run tests and see the results on the console, run this from a Java program:
一旦你拥有了测试,你将要去运行他们。junit提供一套运行和显示结果的工具。运行测试并且在控制台看到结果,在一个Java项目中运行它:
org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);
or this from the command line, with both your test class and junit on the classpath:
或者从下面的命令,将你的测试类和junit放在你的classpath环境变量中:
java org.junit.runner.JUnitCore TestClass1.class [...other test classes...]
You make your JUnit 4 test classes accessible to a TestRunner designed to work with earlier versions of JUnit, declare a static method suite that returns a test.
你制作你的Junit4测试类访问一个TestRunner设计去工作早期的junit版本,描述一个返回一个测试的静态的方法suite
public static junit.framework.Test suite() { return new JUnit4TestAdapter(Example.class); }
How do you verify that code throws exceptions as expected?
Verifying that code completes normally is only part of programming. Making sure the code behaves as expected in exceptional situations is part of the craft of programming too. For example:
怎么去检验代码抛出期望的异常。
检验的代码通常仅仅是程序中的一部分。确信代码
new ArrayList<Object>().get(0);
This code should throw an IndexOutOfBoundsException. The @Test annotation has an optional parameter "expected" that takes as values subclasses of Throwable. If we wanted to verify that ArrayList throws the correct exception, we would write:
@Test(expected= IndexOutOfBoundsException.class) public void empty() { new ArrayList<Object>().get(0); }