首页 > 其他分享 >testng基础知识:注解的执行顺序

testng基础知识:注解的执行顺序

时间:2022-08-21 23:15:45浏览次数:61  
标签:bizTest Thread void 基础知识 basicTest sleep 注解 testng public

testng基础知识:注解的执行顺序

1. 单类,无继承父子关系

  • code:
复制代码
 1 public class basicTest {
 2     @BeforeSuite(alwaysRun = true)
 3     public void beforeSuite_basicTest() throws InterruptedException {
 4         System.out.println("beforeSuite_basicTest");
 5         Thread.sleep(1000);
 6     }
 7 
 8     @AfterSuite(alwaysRun = true)
 9     public void afterSuite_basicTest() throws InterruptedException {
10         System.out.println("afterSuite_basicTest");
11         Thread.sleep(1000);
12     }
13 
14     @BeforeClass(alwaysRun = true)
15     public void beforeClass_basicTest() throws InterruptedException {
16         System.out.println("beforeClass_basicTest");
17         Thread.sleep(1000);
18     }
19 
20     @AfterClass(alwaysRun = true)
21     public void afterClass_basicTest() throws InterruptedException {
22         System.out.println("afterClass_basicTest");
23         Thread.sleep(1000);
24     }
25 
26     @BeforeTest(alwaysRun = true)
27     public void beforeTest_basicTest() throws InterruptedException {
28         System.out.println("beforeTest_basicTest");
29         Thread.sleep(1000);
30     }
31     @AfterTest(alwaysRun = true)
32     public void afterTest_basicTest() throws InterruptedException {
33         System.out.println("afterTest_basicTest");
34         Thread.sleep(1000);
35     }
36 
37     @BeforeMethod(alwaysRun = true)
38     public void beforeMethod_basicTest() throws InterruptedException {
39         System.out.println("beforeMethod_basicTest");
40         Thread.sleep(1000);
41     }
42     @AfterMethod(alwaysRun = true)
43     public void afterMethod_basicTest() throws InterruptedException {
44         System.out.println("afterMethod_basicTest");
45         Thread.sleep(1000);
46     }
47     @Test
48     public void test_basicTest1() throws InterruptedException {
49         System.out.println("test_basicTest1");
50         Thread.sleep(1000);
51     }
52 
53     @Test
54     public void test_basicTest2() throws InterruptedException {
55         System.out.println("test_basicTest2");
56         Thread.sleep(1000);
57     }
58 }
复制代码
  • 执行结果:
复制代码
beforeSuite_basicTest
beforeTest_basicTest
beforeClass_basicTest
beforeMethod_basicTest
test_basicTest1
afterMethod_basicTest
beforeMethod_basicTest
test_basicTest2
afterMethod_basicTest
afterClass_basicTest
afterTest_basicTest
afterSuite_basicTest
复制代码

 

 

2. 2个类,存在继承关系,注解函数不存在同名。

  • code:
复制代码
public class bizTest extends basicTest{
@BeforeClass(alwaysRun </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> beforeClass_bizTest() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"beforeClass_bizTest"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}

@AfterClass(alwaysRun </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> afterClass_bizTest() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"afterClass_bizTest"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}

@BeforeTest(alwaysRun </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> beforeTest_bizTest() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"beforeTest_bizTest"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}
@AfterTest(alwaysRun </span>= <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> afterTest_bizTest() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"afterTest_bizTest"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}
@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> test_bizTest1() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"test_bizTest1"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}

@Test
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">void</span> test_bizTest2() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> InterruptedException {
    System.out.println(</span>"test_bizTest2"<span style="color: rgba(0, 0, 0, 1)">);
    Thread.sleep(</span>1000<span style="color: rgba(0, 0, 0, 1)">);
}

}

复制代码
  • 执行结果:

           注意:此处因执行内容较多,手动进行分行,方面了解执行顺序。

复制代码
beforeSuite_basicTest
beforeSuite_bizTest

beforeTest_basicTest
beforeTest_bizTest

beforeClass_basicTest
beforeClass_bizTest

beforeMethod_basicTest
beforeMethod_bizTest

test_bizTest1

afterMethod_bizTest
afterMethod_basicTest

beforeMethod_basicTest
beforeMethod_bizTest

test_bizTest2

afterMethod_bizTest
afterMethod_basicTest

afterClass_bizTest
afterClass_basicTest

afterTest_bizTest
afterTest_basicTest

afterSuite_bizTest
afterSuite_basicTest

复制代码

 

3. 总结

注解执行顺序:suite, test,  class, method

父/子类执行顺序:先执行父类,再执行子类。

 

https://www.cnblogs.com/heaven1025/p/9835993.html

标签:bizTest,Thread,void,基础知识,basicTest,sleep,注解,testng,public
From: https://www.cnblogs.com/sunny3158/p/16611340.html

相关文章

  • Java注解
    1.注解入门1.1Annotation位于源码中(代码/注释/注解),使用其他工具进行处理的标签注解用来修饰程序的元素,但不会对被修饰的对象有直接的影响只有通过某种配套的工具才......
  • spring5 事务 纯注解开发
    1.TxConfigpackagecom.config;importcom.alibaba.druid.pool.DruidDataSource;importorg.springframework.context.annotation.Bean;importorg.springframework.......
  • 【Java基础】基础知识
    基础基础数据类型值类型引用类型访问修饰符关键字面向对象类与接口内部类内部类的优点内部类有哪些应用场景引用数据类型值类型数值型整数类......
  • Java基础知识
    注释(comment)单行注释://多行注释:/**/文档注释:/***/,javadoc命令可以抽取每个类,方法的文档注释,生成API文档关键字(keyword)关键字是指在程序中已经有特定含......
  • @ImportResource 注解的使用
    本文目录:1.Spring方式的配置文件bean.xml此处随便举个示例,比如说xml中配置了一个helloService,如下所示2.使用@ImportResource注解,引入xml配置3.测试结果......
  • Rust编程基础知识
    1.一般Rust源代码的后缀名是使用.rs表示。源码一定要注意使用utf-8编码。2.代码注释用//,一般用//注释单行,也就是行注释,块注释使用/*和*/包围。3.fn是一个关键字(keyword),......
  • JSON解析器Jackson_java对象转json和JSON解析器Jackson_java对象转json注解
    JSON解析器Jackson_java对象转jsonJSON数据和Java对象的相互转换JSON解析器:常见的解析器:Jsonlib,Gson,fastjson,jackson1.JSON转为Java对象在后......
  • 我的python基础知识点
    0、使用#注释,因为python是脚本语言批量赋值a,b=1,2 //a=1,b=2批量赋值还可以使用序列进行赋值a,b=[1,2] //a=1,b=21、在python中,"helloworld"*3//表示3个这个字符......
  • Java自定义注解
    ​/**作者:呆萌老师*☑csdn认证讲师*☑51cto高级讲师*☑腾讯课堂认证讲师*☑网易云课堂认证讲师*☑华为开发者学堂认证讲师*☑爱奇艺千人名师计划成员*在这里给大......
  • 【热像】热像仪基础知识
    红外热像仪基本知识https://baike.baidu.com/item/热像仪/8551070#reference-[2]-2657998-wrap红外热像仪是一种利用红外热成像技术,通过对标的物的红外辐射探测,并加以......