TestNG执行流程

TestNG基本注解

TestNG支持的注解:

注解 描述
@BeforeSuite 注解的方法将只运行一次,所有测试运行前运行
@AfterSuite 注解的方法将只运行一次,所有测试运行后运行
@BeforeClass 注解的方法将只运行一次,被注释的方法将在当前类的第一个测试方法调用前运行
@AfterClass 注解的方法将只运行一次,被注释的方法将在当前类的最后一个测试方法调用后运行
@BeforeTest 注解的方法将在所有测试方法运行前运行
@AfterTest 注解的方法将在所有测试方法运行后运行
@BeforeGroups The list of groups that this configuration method will run before.
This method is guaranteed to run shortly before the first test method
that belongs to any of these groups is invoked.
@AfterGroups The list of groups that this configuration method will run after.
This method is guaranteed to run shortly after the last test method
that belongs to any of these groups is invoked.
@BeforeMethod 注解的方法将在每个测试方法之前运行
@AfterMethod 注解的方法将在每个测试方法之后运行
@DataProvider Marks a method as supplying data for a test method.
The annotated method must return an Object[][] where
each Object[] can be assigned the parameter list of the test method.
The @Test method that wants to receive data from this DataProvider
needs to use a dataProvider name equals to the name of this annotation.
@Factory Marks a method as a factory that returns objects that will be used by TestNG
as Test classes. The method must return Object[].
@Listeners Defines listeners on a test class.
@Parameters Describes how to pass parameters to a @Test method.
@Test Marks a class or a method as part of the test.

TestNG执行流程

TestNG是执行过程为:

  • @BeforeSuite
  • @BeforeTest
  • @BeforeClass
  • { @BeforeMethod -> @Test -> @AfterMethod }
  • @AfterClass
  • @AfterTest
  • @AfterSuite

TestNGAnnotationTest.java

package me.yezhou;

import org.testng.annotations.*;

public class TestNGAnnotationTest {
    // test case 1
    @Test
    public void testCase1() {
        System.out.println("in test case 1");
    }

    // test case 2
    @Test
    public void testCase2() {
        System.out.println("in test case 2");
    }

    @BeforeMethod
    public void beforeMethod() {
        System.out.println("in beforeMethod");
    }

    @AfterMethod
    public void afterMethod() {
        System.out.println("in afterMethod");
    }

    @BeforeClass
    public void beforeClass() {
        System.out.println("in beforeClass");
    }

    @AfterClass
    public void afterClass() {
        System.out.println("in afterClass");
    }

    @BeforeTest
    public void beforeTest() {
        System.out.println("in beforeTest");
    }

    @AfterTest
    public void afterTest() {
        System.out.println("in afterTest");
    }

    @BeforeSuite
    public void beforeSuite() {
        System.out.println("in beforeSuite");
    }

    @AfterSuite
    public void afterSuite() {
        System.out.println("in afterSuite");
    }
}

testng_annotation.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Suite">
    <test name="test">
        <classes>
            <class name="me.yezhou.TestNGAnnotationTest"/>
        </classes>
    </test>
</suite>

执行结果:

in beforeSuite
in beforeTest
in beforeClass
in beforeMethod
in test case 1
in afterMethod
in beforeMethod
in test case 2
in afterMethod
in afterClass
in afterTest
in afterSuite

===============================================
Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/01/testng-execute-process/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
TestNG执行流程
TestNG基本注解 TestNG支持的注解: 注解 描述 @BeforeSuite 注解的方法将只运行一次,所有测试运行前运行 @AfterSuite 注解的方法将只运行一次,所……
<<上一篇
下一篇>>
文章目录
关闭
目 录