- 前置:
1.本機環境安裝了maven並配置環境變量
2.本機環境安裝了IDEA軟件
3.本機環境安裝了Java jdk 8版本
4.有一定java和maven基礎
因為以上網上例子很多,就不再重復贅述了
一、新建maven項目,在生成的pom.xml中配置所需jar包
<?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> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <groupId>org.example</groupId> <artifactId>Test</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.8.21</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.5</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>dubbo-serialization-fastjson</artifactId> <version>2.6.7</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.24</version> </dependency> <dependency> <groupId>org.uncommons</groupId> <artifactId>reportng</artifactId> <version>1.1.4</version> <scope>test</scope> <exclusions> <exclusion> <groupId>org.testng</groupId> <artifactId>testng</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>3.0</version> <scope>test</scope> </dependency> <dependency> <groupId>com.hynnet</groupId> <artifactId>jxl</artifactId> <version>2.6.12.1</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.5</version> <configuration> <testFailureIgnore>true</testFailureIgnore> <!--這里設置關聯的testNG.xml路徑,項目根目錄下的res文件夾里面--> <suiteXmlFiles> <file>testNG.xml</file> </suiteXmlFiles> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter</value> </property> </properties> </configuration> </plugin> </plugins> </build> </project>
此時右下角會彈出提示,選擇“Import Changes”,會自動下載依賴
接下來要做的就是等它下載完成。
二、生成testng.xml
File->Settings-Plugins 下載Create TestNG XML 插件,重啟IDEA即可。
打開testNG.xml
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <suite name="Suite1"> <!-- 測試名稱會在報告中顯示--> <test name="Get方法測試" verbose="2"> <classes> <!--運行的test類名稱--> <class name="GetDemo" /> </classes> </test> <test name="Post方法測試" verbose="2"> <classes> <class name="PostDemo"/> </classes> </test> </suite>
至此所有配置完成