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
父/子类执行顺序:先执行父类,再执行子类。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)