JUnit4入门学习
1 继承TestCase类的简单测试
1.1 简单测试
1.1.1 继承TestCase
public class Test1 extends TestCase
A test case defines the fixture to run multiple tests. To define a test case
1.1.2 编写测试方法
public void test**() {
}
测试的方法的格式是固定的
1.1.3 如果需要为每个测试方法前执行什么操作,则需要重写setup方法
protected void setUp() {
}
每个测试方法前都会执行这个方法
setUp
testEquals
setUp
testAdd
1.1.4 如果需要为每个测试方法后执行什么操作,则需要重写
protected void tearDown() throws Exception {
}
setUp
testEquals
tearDown
setUp
testAdd
teardown
1.1.5 使用断言来判断测试的结果
assertEquals(12, 12);
assertTrue(result == 5);
1.1.6 使用命令行语句执行各个Test
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
public static Test suite() {
return new TestSuite(SimpleTest.class);
}
TestRunner : A command line based tool to run tests
Test: A Test can be run and collect its results.
TestSuite: A TestSuite is a Composite of Tests. It runs a collection of test cases.
1.2 补充
Fail(string msg)方法:利用JUnit Test跑测试程序的时候,显示的失败提示
2 使用annotations的简单测试
2.1 简单测试
2.1.1 新建一个普通的java类
2.1.2 编写测试方法,保证测试方法的根式是
@Test
public void ***() {
}
测试方法的名称可以不以test开头了
The Test annotation tells JUnit that the public void method to which it is attached can be run as a test case. To run the method, JUnit first constructs a fresh instance of the class then invokes the annotated method. Any exceptions thrown by the test will be reported by JUnit as a failure. If no exceptions are thrown, the test is assumed to have succeeded
The Test annotation supports two optional parameters.The first, expected, declares that a test method should throw an exception. If it doesn't throw an exception or if it throws a different exception than the one declared, the test fails.
The second optional parameter, timeout, causes a test to fail if it takes longer than a specified amount of clock time (measured in milliseconds).
2.1.3 测试方法先添加方法
@Before
public void *() {
}
方法名称可以不叫setup
2.1.4 测试方法后添加方法
@After
public void after()
{ }
2.1.5 使用命令行语句执行各个Test
public static void main (String[] args) {
junit.textui.TestRunner.run(suite());
}
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(ListTest.class);
}
2.2 补充
2.2.1 @BeforeClass
Sometimes several tests need to share computationally expensive setup(like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a public static void no-arg method with @BeforeClass causes it to be run once before any of the test methods in the class. The @BeforeClass methods of superclasses will be run before those the current class.
2.2.2 @Ignore
Sometimes you want to temporarily disable a test or a group of tests. Methods annotated with @Test that are also annotated with @Ignore will not be executed as tests. Also, you can annotate a class containing test methods with @Ignore and none of the containing tests will be executed. Native JUnit 4 test runners should report the number of ignored tests along with the number of tests that ran and the number of tests that failed.
For example:
@Ignore
@Test
public void something() { ...}
@Ignore takes an optional default parameter if you want to record why a test is being ignored:@ Ignore("not ready yet") Test public void something() { ...}@Ignore can also be applied to the test class:
@Ignore
public class IgnoreMe {
@Test
public void test1() { ... }
@Test
public void test2() { ... }
}