testNG注解
本文主要包含testNG注解的使用方法和執行順序兩部分。
一、使用方法
testNG的注解的使用,主要用於方法上 @符號標示,@Test、@afterMethod、@BeforeClass、@BeforeMethod
二、執行順序
單個類:
根據以下代碼執行的結果,我們可以看出來,testNG的執行順序是
@BeforeSuite->@BeforeTest->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass->@AfterTest->@AfterSuite
其中{}內的與多少個@Test,就循環執行多少次。比如下面代碼中有兩個方法被@Test標識。如果存在繼承關系 ,則先執行父類 ,再執行子類。
1 package com.course.testng; 2 3 import org.testng.annotations.*; 4 5 public class BasicAnnotation { 6 @Test 7 public void testCase1() { 8 System.out.println("@Test這是測試用例1"); 9 } 10 11 @BeforeMethod 12 public void beforeMethod() { 13 System.out.println("beforeMethod這是在測試方法之前運行的"); 14 } 15 16 @AfterMethod 17 public void afterMethod() { 18 System.out.println("AfterMethod這是在測試方法之后運行"); 19 } 20 21 @Test 22 public void testCase2() { 23 System.out.println("@Test這是測試用例2"); 24 } 25 26 @BeforeClass 27 public void beforClass() { 28 System.out.println("BeforeClass這是類運行之前的方法"); 29 30 } 31 32 @AfterClass 33 public void afterClass() { 34 System.out.println("afterClass類運行之后的方法"); 35 } 36 37 @BeforeSuite 38 public void beforeSuite() { 39 System.out.println("BeforeSuitec測試套件"); 40 } 41 42 @AfterSuite 43 public void afterSuite() { 44 System.out.println("AfterSuite測試套件"); 45 } 46 47 @BeforeTest 48 public void beforeTest() { 49 System.out.println("BeforeTest這是在每個Test之前運行"); 50 } 51 52 @AfterTest 53 public void afterTest() { 54 System.out.println("AfterTest這是在每個Test之后運行"); 55 } 56 }
以下是運行結果
3 C:\Users\Administrator\.IntelliJIdea2019.3\system\temp-testng-customsuite.xml 4 5 BeforeSuitec測試套件 6 7 BeforeTest這是在每個Test之前運行 8 9 BeforeClass這是類運行之前的方法 10 11 beforeMethod這是在測試方法之前運行的 12 13 @Test這是測試用例1 14 15 AfterMethod這是在測試方法之后運行 16 17 beforeMethod這是在測試方法之前運行的 18 19 @Test這是測試用例2 20 21 AfterMethod這是在測試方法之后運行 22 23 afterClass類運行之后的方法 24 25 AfterTest這是在每個Test之后運行 26 27 AfterSuite測試套件 28 29 =============================================== 30 Default Suite 31 Total tests run: 2, Failures: 0, Skips: 0 32 =============================================== 33 34 Process finished with exit code 0
多個類:
@BeforeSuite(按類順序執行)->@BeforeTest(按類順序執行)->@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass ->@AfterTest->@AfterSuite
黑色部分 按XML中配置的順序執行,即執行你一個類中的@BeforeSuite 第二個類中的@BeforeSuite ,第一個類中的@BeforeTest,第二個類中的@BeforeTest
紅色部分 則需要一個類中的所有test(@BeforeClass->{@BeforeMethod->@Test->@AfterMethod}->@AfterClass)執行完畢之后,再執行第二個類中的test 。
第一個類中的代碼
1 package com.course.testng; 2 3 import org.testng.annotations.*; 4 5 public class BasicAnnotation { 6 @Test 7 public void testCase1() { 8 System.out.println("@Test這是測試用例1"); 9 } 10 11 @BeforeMethod 12 public void beforeMethod() { 13 System.out.println("beforeMethod這是在測試方法之前運行的"); 14 } 15 16 @AfterMethod 17 public void afterMethod() { 18 System.out.println("AfterMethod這是在測試方法之后運行"); 19 } 20 21 @Test 22 public void testCase2() { 23 System.out.println("@Test這是測試用例2"); 24 } 25 26 @BeforeClass 27 public void beforClass() { 28 System.out.println("BeforeClass這是類運行之前的方法"); 29 30 } 31 32 @AfterClass 33 public void afterClass() { 34 System.out.println("afterClass類運行之后的方法"); 35 } 36 37 @BeforeSuite 38 public void beforeSuite() { 39 System.out.println("BeforeSuitec測試套件"); 40 } 41 42 @AfterSuite 43 public void afterSuite() { 44 System.out.println("AfterSuite測試套件"); 45 } 46 47 @BeforeTest 48 public void beforeTest() { 49 System.out.println("BeforeTest這是在每個Test之前運行"); 50 } 51 52 @AfterTest 53 public void afterTest() { 54 System.out.println("AfterTest這是在每個Test之后運行"); 55 } 56 }
第二個類中的代碼
1 package com.course.testng; 2 3 import org.testng.annotations.*; 4 5 public class CpBasicAnnotation { 6 @Test 7 public void testCase1() { 8 System.out.println("@Test這是測試用例1__________CpBasicAnnotation"); 9 } 10 11 @BeforeMethod 12 public void beforeMethod() { 13 System.out.println("beforeMethod這是在測試方法之前運行的__________CpBasicAnnotation"); 14 } 15 16 @AfterMethod 17 public void afterMethod() { 18 System.out.println("AfterMethod這是在測試方法之后運行__________CpBasicAnnotation"); 19 } 20 21 @Test 22 public void testCase2() { 23 System.out.println("@Test這是測試用例__________CpBasicAnnotation2"); 24 } 25 26 @BeforeClass 27 public void beforClass() { 28 System.out.println("BeforeClass這是類運行之前的方法__________CpBasicAnnotation"); 29 30 } 31 32 @AfterClass 33 public void afterClass() { 34 System.out.println("afterClass類運行之后的方法__________CpBasicAnnotation"); 35 } 36 37 @BeforeSuite 38 public void beforeSuite() { 39 System.out.println("BeforeSuitec測試套件__________CpBasicAnnotation"); 40 } 41 42 @AfterSuite 43 public void afterSuite() { 44 System.out.println("AfterSuite測試套件__________CpBasicAnnotation"); 45 } 46 47 @BeforeTest 48 public void beforeTest() { 49 System.out.println("BeforeTest這是在每個Test之前運行__________CpBasicAnnotation"); 50 } 51 52 @AfterTest 53 public void afterTest() { 54 System.out.println("AfterTest這是在每個Test之后運行__________CpBasicAnnotation"); 55 } 56 57 }
xml配置文件
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <suite name="suitname"> 3 <test name="runAll"> 4 <classes> 5 <class name="com.course.testng.BasicAnnotation"/> 6 <class name="com.course.testng.CpBasicAnnotation"/> 7 </classes> 8 </test> 9 </suite>
運行結果如下
1 BeforeSuitec測試套件 2 BeforeSuitec測試套件__________CpBasicAnnotation 3 BeforeTest這是在每個Test之前運行 4 BeforeTest這是在每個Test之前運行__________CpBasicAnnotation 5 6 BeforeClass這是類運行之前的方法 7 beforeMethod這是在測試方法之前運行的 8 @Test這是測試用例1 9 AfterMethod這是在測試方法之后運行 10 beforeMethod這是在測試方法之前運行的 11 @Test這是測試用例2 12 AfterMethod這是在測試方法之后運行 13 afterClass類運行之后的方法 14 15 16 BeforeClass這是類運行之前的方法__________CpBasicAnnotation 17 beforeMethod這是在測試方法之前運行的__________CpBasicAnnotation 18 @Test這是測試用例1__________CpBasicAnnotation 19 AfterMethod這是在測試方法之后運行__________CpBasicAnnotation 20 beforeMethod這是在測試方法之前運行的__________CpBasicAnnotation 21 @Test這是測試用例__________CpBasicAnnotation2 22 AfterMethod這是在測試方法之后運行__________CpBasicAnnotation 23 afterClass類運行之后的方法__________CpBasicAnnotation 24 25 26 AfterTest這是在每個Test之后運行 27 AfterTest這是在每個Test之后運行__________CpBasicAnnotation 28 AfterSuite測試套件 29 AfterSuite測試套件__________CpBasicAnnotation 30 31 =============================================== 32 suitname 33 Total tests run: 4, Failures: 0, Skips: 0 34 =============================================== 35 Process finished with exit code 0