• testNG注解的使用和执行顺序


    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:UsersAdministrator.IntelliJIdea2019.3system	emp-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
  • 相关阅读:
    with open 向文件的某一固定行,追加内容
    静态语言 与 动态语言 的区别
    ELK
    matplotlib绘图
    django用户认证
    django+uwsgi+nginx 部署生产环境
    图片验证码+session
    ajax
    form
    middleware中间件
  • 原文地址:https://www.cnblogs.com/linxinmeng/p/12590337.html
Copyright © 2020-2023  润新知