testNG注解与执行

作者: 迷糊银儿 | 来源:发表于2018-11-02 18:25 被阅读48次

项目地址:https://gitee.com/neimenggudaxue/BasicTest

一、testNG注解

1.@BeforeSuite
被@BeforeSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之前运行。
2.@AfterSuite
被@AfterSuite注解的方法,将会在testng定义的xml根元素里面的所有执行之后运行。
3.@BeforeTest
被@BeforeTest注解的方法,将会在一个元素定义的所有里面所有测试方法执行之前运行。
4.@AfterTest
被@AfterTest注解的方法,将会在一个元素定义的所有里面所有的测试方法执行之后运行。
5.@BeforeClass
被@BeforeClass注解的方法,将会在当前测试类的第一个测试方法执行之前运行。
6.@AfterClass
被@AfterClass注解的方法,将会在当前测试类的最后一个测试方法执行之后运行。
7.@BeforeMethod
被@BeforeMethod注解的方法,将会在当前测试类的每一个测试方法执行之前运行。
8.@AfterMethod
被@AfterMethod注解的方法,将会在当前测试类的每一个测试方法执行之后运行。

二、testNG.xml文件的执行

pom.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>qufang</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.30</version>
        </dependency>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.11</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <compilerArgs>
                        <arg>-Xlint:unchecked</arg>
                        <arg>-Xlint:deprecation </arg>
                    </compilerArgs>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.5</version>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                    <suiteXmlFiles>
                        <file>res/testNG.xml</file>
                    </suiteXmlFiles>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

testNG.xml文件

<?xml version="1.0" encoding="utf-8" ?>
<suite name="Suite1" verbose="1">
    <test name="test1">
        <classes>
            <class name="testNG.TestNGAnnotationTest" />
        </classes>
    </test>
</suite>

TestNGAnnotationTest类

package testNG;

import org.testng.annotations.*;

/**
 * @Description: 接口:testng注解测试
 * @Author: qufang
 * @Date: Created in 下午5:44 2018/11/2
 */
public class TestNGAnnotationTest {
    @BeforeSuite
    public void beforeSuite(){
        System.out.println(this.getClass().getName()+"BeforeSuite");
    }

    @BeforeTest
    public void beforeTest(){
        System.out.println(this.getClass().getName()+"BeforeTest");
    }

    @BeforeClass
    public void beforeClass(){
        System.out.println(this.getClass().getName()+"BeforeClass");
    }

    @BeforeMethod
    public void beforeMethod(){
        System.out.println(this.getClass().getName()+"BeforeMethod");
    }

    @AfterMethod
    public void aftermethod(){
        System.out.println(this.getClass().getName()+"AfterMethod");
    }

    @AfterClass
    public void afterClass(){
        System.out.println(this.getClass().getName()+"AfterClass");
    }

    @AfterTest
    public void afterTest(){
        System.out.println(this.getClass().getName()+"AfterTest");
    }

    @AfterSuite
    public void afterSuit(){
        System.out.println(this.getClass().getName()+"AfterSuite");
    }

    @Test
    public void test1(){
        System.out.println(this.getClass().getName()+" test1");
    }

    @Test
    public void test2(){
        System.out.println(this.getClass().getName()+" test2");
    }
}

选中testNG.xml文件,点击run ,结果

testNG.TestNGAnnotationTestBeforeSuite
testNG.TestNGAnnotationTestBeforeTest
testNG.TestNGAnnotationTestBeforeClass
testNG.TestNGAnnotationTestBeforeMethod
testNG.TestNGAnnotationTest test1
testNG.TestNGAnnotationTestAfterMethod
testNG.TestNGAnnotationTestBeforeMethod
testNG.TestNGAnnotationTest test2
testNG.TestNGAnnotationTestAfterMethod
testNG.TestNGAnnotationTestAfterClass
testNG.TestNGAnnotationTestAfterTest
testNG.TestNGAnnotationTestAfterSuite

所以testNG.xml文件的执行实质就是依次执行冠以@Test注解的方法。而每执行一个@Test方法,都是依据<一>中的注解含义而形成的执行顺序

相关文章

  • testNG注解与执行

    项目地址:https://gitee.com/neimenggudaxue/BasicTest 一、testNG注...

  • TestNG注解使用与测试技巧

    TestNG注解的使用 TestNG执行结果顺序 其中的BeforeMethod/AfterMethod�会在每个...

  • TestNG

    注解 执行顺序 testng.xml tag详解

  • TestNG并行执行测试

    TestNG中实现多线程并行执行,可以通过几种方法 testng.xml中配置 @Test注解 @DataProv...

  • 2.testng杂记

    1.testng中主要常用的注解方法,如图1所示: 2.testng中注解方法执行的先后顺序,如图2所示:

  • (十二)TestNG学习之路—注解转换器

    前言 TestNG允许您在测试执行时修改所有注解(@Test,@DataProvider,@Factory等)的内...

  • @findy注解,testNG控制执行顺序

    @findy注解 1、针对每个页面新建一个类 2、在类中使用@FindBy注解定位元素,并把定位结果存在变量中 4...

  • 测试框架TestNG使用介绍

    今天分享TestNG测试框架的基础知识,使用TestNG的优点,TestNG的基本注解如何使用,套件、忽略、异常、...

  • testNG

    1.testng.xml结构规则 2.TestNG注解 用于在测试类中注解: 3.Java文件的测试用例中通过获取...

  • 测试-TestNG.XML文件case1

    一、TestNG按顺序执行case 在testng.xml中,可以控制测试用例按顺序执行。 二、TestNG分组执...

网友评论

    本文标题:testNG注解与执行

    本文链接:https://www.haomeiwen.com/subject/oqvcxqtx.html