Junit 4允许通过使用测试套件类批量运行测试类 . 为一套测试类创建一个测试套件,要为测试类添加以下注解:
当运行时,所有包含在@SuiteClasses注解内的所有测试类都会被执行。
在本例中, ArithmeticTest.java and GeometricTest.java 将会被Junit测试套件类AllTest.java一起执行 Arithmetic.java package in.co.javatutorials; /** * @author javatutorials.co.in */ public class Arithmetic { public int add(int i, int j) { return i + j; } } Geometric.java package in.co.javatutorials; /** * @author javatutorials.co.in */ public class Geometry { public int squareArea(int length) { return length * length; } } ArithmeticTest.java package in.co.javatutorials; import static org.junit.Assert.*; import org.junit.Test; /** * @author javatutorials.co.in */ public class ArithmeticTest { @Test public void testAdd() { Arithmetic arithmetic = new Arithmetic(); int actualResult = arithmetic.add(1 , 2); int expectedResult = 3; assertEquals(expectedResult, actualResult); } } GeometricTest.java package in.co.javatutorials; import static org.junit.Assert.*; import org.junit.Test; /** * @author javatutorials.co.in */ public class GeometryTest { @Test public void testSquareArea() { Geometry geometry = new Geometry(); int actualResult = geometry.squareArea(2); int expectedResult = 4; assertEquals(expectedResult, actualResult); } } AllTests.java Test Suite Class package in.co.javatutorials; import org.junit.runner.RunWith; import org.junit.runners.Suite; import org.junit.runners.Suite.SuiteClasses; /** * @author javatutorials.co.in */ @RunWith(Suite.class) @SuiteClasses({ ArithmeticTest.class, GeometryTest.class }) public class AllTests { }
结果输出
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
本文出处为 http://blog.csdn.net/luanlouis,转载请注明出处,谢谢!