public class ComplexTest extends TestCase {
private Complex a;
private Complex b;
protected void setUp() {
a = new Complex(1, -1);
b = new Complex(2, 5);
}
}
(b)
public void testComplexEquality()
Complex expected = new Complex(1, -1);
assertEquals(expected, a);
}
public void testComplexAddition() {
Complex expected = new Complex(3, 4);
assertEquals(expected, a.add(b));
}
public void testComplexMultiplication() {
Complex expected = new Complex(1*2 - (-1)*5,1*5 + (-1)*2);
assertEquals(expected, a.multiply(b));
}
(c)
public static Testsuite() {
return new TestSuite(ComplexTest.class);
}
Figure 1 一个复数类例子:(a)JUnit测试的setup,(b)JUnit测试的实例,(c)在一个类中为所有实例动态得创建一个测试组(test suite)
文章来源于领测软件测试网 https://www.ltesting.net/