用maven將TestNG框架程式碼打成jar包並執行測試-可持續整合
1.maven 依賴 TestNG:
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.11</version>
<scope>compile</scope>
</dependency>
注:<scope>compile</scope>或者預設不填,表示依賴打包到專案中。
2.<!-- maven test 執行testng.xml -->
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
3.<!-- maven 打依賴jar包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.uiautotest.platformsys.JavaRunXml</mainClass>//執行主程式
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
4.testng.xml檔案放入要執行的jar包中
cmd執行命令:
java -jar jar包名.jar
執行後 test-output 檔案生成在 與 testng.xml 同目錄中。
附完整xml檔案:
<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.uiautotest</groupId> <artifactId>platformsys</artifactId> <version>0.0.1</version> <packaging>jar</packaging> <name>platformsys</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <!-- 依賴包 --> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.11</version> <scope>compile</scope> </dependency> <dependency> <groupId>selenium-java</groupId> <artifactId>selenium-java </artifactId> <version>2.53.0 </version> </dependency> </dependencies> <!-- build構建 --> <build> <plugins> <!-- maven-surefire-plugin執行test測試包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.12.4</version> <configuration> <!-- <includes> <include>../*.java</include> </includes>自動識別和執行src/test目錄 --> <includes> <include>AppTest.java</include> <include>CppTest.java</include> <!-- 跳過測試 --> <skip></skip> <skipTests>false</skipTests> </includes> <!-- maven test 執行testng.xml --> <suiteXmlFiles> <suiteXmlFile>testng.xml</suiteXmlFile> </suiteXmlFiles> </configuration> </plugin> <!-- maven 打依賴jar包 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.0.0</version> <configuration> <archive> <manifest> <addClasspath>true</addClasspath> <mainClass>com.uiautotest.platformsys.JavaRunXml</mainClass> </manifest> </archive> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>