TestNG+Maven+IDEA环境搭建
阅读原文时间:2021年04月20日阅读:1

TestNG+Maven+IDEA环境搭建

前言:

主要进行TestNG测试环境的搭建
所需环境:
1、IDEA UItimate
2、JDK
3、Maven

一、创建工程
File –>new –>Project–>next–>finish

上面两项名称自己定义

最后工程目录

二、导入相关依赖包和插件

1)添加testng依赖包
在pom.xml文件添加

   <dependencies>
        <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.8</version>
        </dependency>
    </dependencies>

2)添加编译插件和执行测试插件

    <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>
                        <!--<arg>endorseddirs=${endorsed.dir}</arg>-->
                    </compilerArgs>
                </configuration>
            </plugin>
             添加插件 关联testNg.xml
            <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>
                    <!--<workingDirectory>target/</workingDirectory>-->
                </configuration>
            </plugin>
        </plugins>
    </build>

具体的maven插件请查看

http://www.infoq.com/cn/news/2011/04/xxb-maven-7-plugin/
http://www.infoq.com/cn/news/2011/05/xxb-maven-8-plugin

三、创建测试类
在新建测试类,代码如下

public class TestDemo  
  @Test
    public void testcase1(){
        Assert.assertTrue(false);
        System.out.println("testcase1");
    }
    @Test
    public void testcase2(){
        Assert.assertTrue(true);
        System.out.println("testcase1");
    }
}

IDEA执行编译

编译情况

四、编写testNG.xml

创建一个新的文件夹 res,里面创建testNG.xml (该文件名自己可以定义,相应需要在pom.xml的文件修改对应文件名)
编写testNG.xml

<?xml version="1.0" encoding="utf-8" ?>
<suite name="testproj" parallel="false">
    <test name="testDemo1">
        <!--<packages>-->
            <!--<package name="com.testproj.Demo"/>-->
        <!--</packages>-->
        <classes>
            <class name="com.testproj.Demo.TestDemo1"></class>
        </classes>
    </test>
</suite>

差不多搞定了,我们来看下使用maven执行测试

在IDEA控制台Terminal输入

mvn -f pom.xml clean test  -DxmlFileName=testNG.xml

编译结果

测试报告

target/surefire-reports/com.testproj.Demo.TestDemo1文件夹下打开报告

到此创建TestNG+Maven+IDEA环境完成。


在此过程出现的问题

找了段时间,发现是testNG.xml文件的classes 写成了classs,导致了错误!

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章