TestNG+Maven+IDEA环境搭建

时间:2023-11-14 11:00:38

TestNG+Maven+IDEA环境搭建

前言:

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

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

TestNG+Maven+IDEA环境搭建

TestNG+Maven+IDEA环境搭建

上面两项名称自己定义

最后工程目录 
TestNG+Maven+IDEA环境搭建

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

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

   <dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
</dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

具体的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");
}
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

IDEA执行编译

TestNG+Maven+IDEA环境搭建

编译情况

TestNG+Maven+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>

TestNG+Maven+IDEA环境搭建

TestNG+Maven+IDEA环境搭建

TestNG+Maven+IDEA环境搭建


TestNG+Maven+IDEA环境搭建

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

在IDEA控制台Terminal输入

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

编译结果

TestNG+Maven+IDEA环境搭建

测试报告

target/surefire-reports/com.testproj.Demo.TestDemo1文件夹下打开报告 
TestNG+Maven+IDEA环境搭建

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