TestNG+Maven+IDEA環境搭建


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>
  • 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.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環境完成。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM