• JUnit注释的执行顺序


    注释就好像你可以在你的代码中添加并且在方法或者类中应用的元标签。JUnit 中的这些注释为我们提供了测试方法的相关信息,哪些方法将会在测试方法前后应用,哪些方法将会在所有方法前后应用,哪些方法将会在执行中被忽略。

    序号 注释和描述
    1 @Test 这个注释说明依附在 JUnit 的 public void 方法可以作为一个测试案例。
    2 @Before 有些测试在运行前需要创造几个相似的对象。在 public void 方法加该注释是因为该方法需要在 test 方法前运行。
    3 @After 如果你将外部资源在 Before 方法中分配,那么你需要在测试运行后释放他们。在 public void 方法加该注释是因为该方法需要在 test 方法后运行。
    4 @BeforeClass 在 public void 方法加该注释是因为该方法需要在类中所有方法前运行。
    5 @AfterClass 它将会使方法在所有测试结束后执行。这个可以用来进行清理活动。
    6 @Ignore 这个注释是用来忽略有关不需要执行的测试的。



    创建一个测试案例

    package Annotation;
    
    import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
    import org.junit.*;
    
    public class JUnitAnnotation {
    
        @Test
        public void testOne(){
            System.out.println("TestOne");
        }
    
        @Test
        public void testTwo(){
            System.out.println("TestTwo");
        }
    
        @Before
        public void TestBefore(){
            System.out.println("Before");
        }
    
        @BeforeClass
        public static void TestBeforeClass(){
            System.out.println("BeforeClass");
        }
    
        @After
        public void TestAfter(){
            System.out.println("After");
        }
    
        @AfterClass
        public static void TestAfterClass(){
            System.out.println("AfterClass");
        }
    
        @Ignore
        public void TestIgnore(){
            System.out.println("Ignore");
        }
    
    
    
    }
    
    



    执行测试案例

    package Annotation;
    
    import org.junit.runner.JUnitCore;
    import org.junit.runner.Result;
    import org.junit.runner.notification.Failure;
    
    public class TestRunner {
        public static void main(String[] args) {
            Result result = JUnitCore.runClasses(JUnitAnnotation.class);
    
            for(Failure failure:result.getFailures()){
                System.out.println(failure.toString());
            }
    
            System.out.println(result.wasSuccessful());
        }
    }
    
    



    执行结果



    结果分析

    • beforeClass() 方法首先执行,并且只执行一次。
    • afterClass() 方法最后执行,并且只执行一次。
    • before() 方法针对每一个测试用例执行,但是是在执行测试用例之前。
    • after() 方法针对每一个测试用例执行,但是是在执行测试用例之后。
    • 在 before() 方法和 after() 方法之间,执行每一个测试用例。
  • 相关阅读:
    Qt 4.7
    CMake记录(一)
    CMake记录(二)
    a critical review of preetham skylight model 笔记
    在webform中清空多个控件的值的简单方法
    Java学习网站 [摘自互联网]
    关于Google API的学习
    DataGrid连接的
    http://www.dnc.com.cn
    在DataGrid中显示数据库中的图片
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12728287.html
Copyright © 2020-2023  润新知