栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

TestNg的各种Before+各种After+执行优先级

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

TestNg的各种Before+各种After+执行优先级

TestNg的各种Before+各种After+执行优先级 准备三个测试类
package com.newcrud.testngTest;

import org.testng.annotations.*;

public class TestOne {
    @Test
    public void testOne(){
        System.out.println("testOne");
    }
    @Test
    public void testTwo(){
        System.out.println("testTwo");
    }
    @Test(groups = "one")
    public void testThree(){
        System.out.println("testThree");
    }
    @Test(groups = "one")
    public void testFour(){
        System.out.println("testFour");
    }
    @Test(groups = "two")
    public void testFive(){
        System.out.println("testFive");
    }
    @Test(groups = "two")
    public void testSix(){
        System.out.println("testSix");
    }
    @BeforeSuite
    public void testSeven(){
        System.out.println("@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行");
    }
    @AfterSuite
    public void testEight(){
        System.out.println("@AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。");
    }
    @BeforeClass
    public void testNine(){
        System.out.println("@BeforeClass,在当前类的第一个测试方法之前运行。");
    }
    @AfterClass
    public void testTen(){
        System.out.println("@AfterClass,运行当前类中的所有测试方法之后都运行。");
    }
    @BeforeGroups(groups = {"one","two"})
    public void testEleven(){
        System.out.println("@BeforeGroups:在调用属于该组的第一个测试方法之前运行.");
    }
    @AfterGroups(groups = {"one","two"})
    public void testTwelve(){
        System.out.println("@AfterGroups:在调用属于组的最后一个测试方法之后运行。");
    }
    @BeforeTest
    public void testThirteen(){
        System.out.println("@BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。");
    }
    @AfterTest
    public void testFourteen(){
        System.out.println("@AfterTest - 对于套件测试,在运行属于标签内的类的所有测试方法都已运行之后运行");
    }
    @BeforeMethod
    public void testFifteen(){
        System.out.println("@BeforeMethod - 在每个测试方法之前运行。");
    }
    @AfterMethod
    public void testSixteen(){
        System.out.println("@AfterMethod - 在每个测试方法之后运行。");
    }

}
package com.newcrud.testngTest;

import org.testng.annotations.*;

public class TestTwo {
    @Test
    public void testOne(){
        System.out.println("TestTwo的testOne");
    }
}
package com.newcrud.testngTest;

import org.testng.annotations.Test;

public class TestThree {
    @Test
    public void testOne(){
        System.out.println("TestThree的testOne");
    }
}
执行的优先级

执行TestOne,查看结果,暂时先不看Suite和Test相关的注解,重点观察Groups的

@BeforeSuite,对于套件测试,在此套件中的所有测试运行之前运行
@BeforeTest - 对于套件测试,在运行属于标签内的类的任何测试方法之前运行。
@BeforeClass,在当前类的第一个测试方法之前运行。
@BeforeGroups:在调用属于该组的第一个测试方法之前运行.
@BeforeMethod - 在每个测试方法之前运行。
testFive
@AfterMethod - 在每个测试方法之后运行。
@BeforeGroups:在调用属于该组的第一个测试方法之前运行.
@BeforeMethod - 在每个测试方法之前运行。
testFour
@AfterMethod - 在每个测试方法之后运行。
@BeforeMethod - 在每个测试方法之前运行。
testOne
@AfterMethod - 在每个测试方法之后运行。
@BeforeMethod - 在每个测试方法之前运行。
testSix
@AfterMethod - 在每个测试方法之后运行。
@AfterGroups:在调用属于组的最后一个测试方法之后运行。
@BeforeMethod - 在每个测试方法之前运行。
testThree
@AfterMethod - 在每个测试方法之后运行。
@AfterGroups:在调用属于组的最后一个测试方法之后运行。
@BeforeMethod - 在每个测试方法之前运行。
testTwo
@AfterMethod - 在每个测试方法之后运行。
@AfterClass,运行当前类中的所有测试方法之后都运行。
@AfterTest - 对于套件测试,在运行属于标签内的类的所有测试方法都已运行之后运行
@AfterSuite,对于套件测试,在此套件中的所有测试运行之后运行。

刚开始我学习的时候在这里甚是迷惑,明明one的第一个为Three第二个为Four,two的第一个为Five第二个为Six。那BeforeGroups应该在Three和Five前,AfterGroups应该在Four和Six后,但是结果确是BeforeGroups在Five和Four前,AfterGroupsThree和Six后。然后就钻牛角尖出不来了,直到后来发现整个带@Test的执行顺序都是按照one、two、three来的,是按照首字母的前后来排序的,好吧,通过搜索引擎也确认了这个点,困扰了我半个多小时。

Suite和Test
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/310296.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号