testNG注解與執(zhí)行

項(xiàng)目地址:https://gitee.com/neimenggudaxue/BasicTest

一、testNG注解

1.@BeforeSuite
被@BeforeSuite注解的方法,將會(huì)在testng定義的xml根元素里面的所有執(zhí)行之前運(yùn)行。
2.@AfterSuite
被@AfterSuite注解的方法,將會(huì)在testng定義的xml根元素里面的所有執(zhí)行之后運(yùn)行。
3.@BeforeTest
被@BeforeTest注解的方法,將會(huì)在一個(gè)元素定義的所有里面所有測試方法執(zhí)行之前運(yùn)行。
4.@AfterTest
被@AfterTest注解的方法,將會(huì)在一個(gè)元素定義的所有里面所有的測試方法執(zhí)行之后運(yùn)行。
5.@BeforeClass
被@BeforeClass注解的方法,將會(huì)在當(dāng)前測試類的第一個(gè)測試方法執(zhí)行之前運(yùn)行。
6.@AfterClass
被@AfterClass注解的方法,將會(huì)在當(dāng)前測試類的最后一個(gè)測試方法執(zhí)行之后運(yùn)行。
7.@BeforeMethod
被@BeforeMethod注解的方法,將會(huì)在當(dāng)前測試類的每一個(gè)測試方法執(zhí)行之前運(yùn)行。
8.@AfterMethod
被@AfterMethod注解的方法,將會(huì)在當(dāng)前測試類的每一個(gè)測試方法執(zhí)行之后運(yùn)行。

二、testNG.xml文件的執(zhí)行

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文件,點(diǎn)擊run ,結(jié)果

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文件的執(zhí)行實(shí)質(zhì)就是依次執(zhí)行冠以@Test注解的方法。而每執(zhí)行一個(gè)@Test方法,都是依據(jù)<一>中的注解含義而形成的執(zhí)行順序

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容