1. testNG+Maven 环境搭建

Posted RoyFans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1. testNG+Maven 环境搭建相关的知识,希望对你有一定的参考价值。

一:使用的工具 :

  • TestNG 6.9.10
  • Maven 3.5
  • IDEA

二:创建maven项目,在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>com.ng</groupId>
    <artifactId>com.ng</artifactId>
    <version>1.0-SNAPSHOT</version>
    <dependencies>

        <!-- https://mvnrepository.com/artifact/org.testng/testng -->
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.10</version>
            <scope>test</scope>
        </dependency>

    </dependencies>


</project>

三、第一个实例

package com.hornbill;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;

/**
 * 执行顺序
 * 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
 */
public class TestFuc {

    // 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");
    }
}

 

四、点击右键执行,执行结果如下:

"C:Program FilesJavajdk1.8.0_74injava" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:Program FilesJetBrainsIntelliJ IDEA 2018.1libidea_rt.jar=60165:C:Program FilesJetBrainsIntelliJ IDEA 2018.1in" -Dfile.encoding=UTF-8 -classpath "C:Program FilesJetBrainsIntelliJ IDEA 2018.1libidea_rt.jar;C:Program FilesJetBrainsIntelliJ IDEA 2018.1plugins	estnglib	estng-plugin.jar;C:Program FilesJavajdk1.8.0_74jrelibcharsets.jar;C:Program FilesJavajdk1.8.0_74jrelibdeploy.jar;C:Program FilesJavajdk1.8.0_74jrelibextaccess-bridge-64.jar;C:Program FilesJavajdk1.8.0_74jrelibextcldrdata.jar;C:Program FilesJavajdk1.8.0_74jrelibextdnsns.jar;C:Program FilesJavajdk1.8.0_74jrelibextjaccess.jar;C:Program FilesJavajdk1.8.0_74jrelibextjfxrt.jar;C:Program FilesJavajdk1.8.0_74jrelibextlocaledata.jar;C:Program FilesJavajdk1.8.0_74jrelibext
ashorn.jar;C:Program FilesJavajdk1.8.0_74jrelibextsunec.jar;C:Program FilesJavajdk1.8.0_74jrelibextsunjce_provider.jar;C:Program FilesJavajdk1.8.0_74jrelibextsunmscapi.jar;C:Program FilesJavajdk1.8.0_74jrelibextsunpkcs11.jar;C:Program FilesJavajdk1.8.0_74jrelibextzipfs.jar;C:Program FilesJavajdk1.8.0_74jrelibjavaws.jar;C:Program FilesJavajdk1.8.0_74jrelibjce.jar;C:Program FilesJavajdk1.8.0_74jrelibjfr.jar;C:Program FilesJavajdk1.8.0_74jrelibjfxswt.jar;C:Program FilesJavajdk1.8.0_74jrelibjsse.jar;C:Program FilesJavajdk1.8.0_74jrelibmanagement-agent.jar;C:Program FilesJavajdk1.8.0_74jrelibplugin.jar;C:Program FilesJavajdk1.8.0_74jrelib
esources.jar;C:Program FilesJavajdk1.8.0_74jrelib
t.jar;D:comng	arget	est-classes;D:comng	argetclasses;C:Usersadmin.m2
epositoryorg	estng	estng6.9.10	estng-6.9.10.jar;C:Usersadmin.m2
epositorycomeustjcommander1.48jcommander-1.48.jar;C:Usersadmin.m2
epositoryorgeanshellsh2.0b4sh-2.0b4.jar;C:Program FilesJetBrainsIntelliJ IDEA 2018.1plugins	estnglibjcommander.jar" org.testng.RemoteTestNGStarter -usedefaultlisteners false -socket60164 @[email protected]:UsersadminAppDataLocalTempidea_working_dirs_testng.tmp -temp C:UsersadminAppDataLocalTempidea_testng.tmp
[TestNG] Running:
  C:Usersadmin.IntelliJIdea2018.1system	emp-testng-customsuite.xml
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

 

以上是关于1. testNG+Maven 环境搭建的主要内容,如果未能解决你的问题,请参考以下文章

Appium TestNg Maven Android Eclipse java自动化环境搭建

selenium+java+testNG+maven环境搭建

selenium+jenkins+maven+testNG搭建持续集成环境

testng环境搭建

java+selenium+testng+reportng+jenkins自动化环境搭建

Mac系统搭建java+selenium+testng环境