sunny123456

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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
posted on 2022-08-21 23:01  sunny123456  阅读(287)  评论(0编辑  收藏  举报