zoukankan      html  css  js  c++  java
  • 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() 方法之间,执行每一个测试用例。
  • 相关阅读:
    str_split — 将字符串转换为数组
    str_replace — 子字符串替换
    str_pad — 使用另一个字符串填充字符串为指定长度
    parse_str — 将字符串解析成多个变量
    number_format — 以千位分隔符方式格式化一个数字
    企业邮箱的优势有哪些?使用企业邮箱的好处
    TOM VIP邮箱,化繁为简,在微信里收发邮件
    邮箱办公神操作,让办公更自在,沟通无边界!
    常用的企业邮箱有哪些?企业邮箱有哪几种?
    外贸企业邮箱批发,收费企业邮箱与免费企业邮箱区别
  • 原文地址:https://www.cnblogs.com/lyd447113735/p/12728287.html
Copyright © 2011-2022 走看看