JUnit运行流程

package junit;

import static org.junit.Assert.*;

import org.junit.*;

/**
 * Created by guoli on 2017/4/18 0018.
 * aW1laWxpZ2VAZ21haWwuY29tCiAgICAgcXE6Mjg5MTY4NTEKICAgIHpoYW5nZ3VvbGkuY24=
 */
public class FlowTest {

    @BeforeClass
    public static void setUpBeforeClass() throws Exception {
        System.out.println("BeforeClass...");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("AfterClass...");
    }

    @Before
    public void setUp() throws Exception {
        System.out.println("Before...");
    }

    @After
    public void tearDown() throws Exception {
        System.out.println("After...");
    }

    @Test
    public void test1() throws Exception {
        System.out.println("test1.....");
    }

    @Test
    public void test2() throws Exception {
        System.out.println("test2.....");
    }
}

运行FlowTest类,输出如下内容:

BeforeClass...
Before...
test1.....
After...
Before...
test2.....
After...
AfterClass...

结论:@BeforeClass修饰的方法在所有测试方法执行前执行,可以用来加载配置文件,进行某些初始化;@AfterClass修饰的方法在所有测试方法执行完成后执行,用来清理资源;@Before@After会在每个测试方法执行前后各执行一次

标签: junit

相关文章推荐: