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 = true) public void beforeClass_bizTest() throws InterruptedException { System.out.println("beforeClass_bizTest"); Thread.sleep(1000); } @AfterClass(alwaysRun = true) public void afterClass_bizTest() throws InterruptedException { System.out.println("afterClass_bizTest"); Thread.sleep(1000); } @BeforeTest(alwaysRun = true) public void beforeTest_bizTest() throws InterruptedException { System.out.println("beforeTest_bizTest"); Thread.sleep(1000); } @AfterTest(alwaysRun = true) public void afterTest_bizTest() throws InterruptedException { System.out.println("afterTest_bizTest"); Thread.sleep(1000); } @Test public void test_bizTest1() throws InterruptedException { System.out.println("test_bizTest1"); Thread.sleep(1000); } @Test public void test_bizTest2() throws InterruptedException { System.out.println("test_bizTest2"); Thread.sleep(1000); } }
- 执行结果:
注意:此处因执行内容较多,手动进行分行,方面了解执行顺序。
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
父/子类执行顺序:先执行父类,再执行子类。