背景:
本地编写的很多testNG测试用例,可能需要对接其他人员,运用其他形式执行,例如将测试用例达成jar包,由运维执行,需要提供执行命令,提供前需要本地先验证是否可执行通过。
一、maven配置
1.官网下载maven(https://maven.apache.org/download.cgi),下载最新版本,解压到制定文件夹:
2.配置环境变量,以win10为例:
新建系统变量MAVEN_HOME,填写下载解压后的文件地址,注意是bin目录的上一级目录
编辑Path变量,在末尾加上:
3.检验,cmd,进入终端,输入mvn -v ,显示以下信息说明配置完成
二、Idea执行配置【注意配置好maven/jdk环境(JDK下载参照以前教程)变量后需要重启idea】
1.在pom.xml引入依赖:
<build> <sourceDirectory>src/main/java</sourceDirectory> <testSourceDirectory>src/main/java</testSourceDirectory> <finalName>${project.artifactId}</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>7</source> <target>7</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.6</version> <!--jar--> <configuration> <argLine> -javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/1.8.10/aspectjweaver-1.8.10.jar" </argLine> <testFailureIgnore>true</testFailureIgnore> <!-- <testNGArtifactName>org.testng:testng</testNGArtifactName>--> <forkMode>once</forkMode> <!--<skipTests>true</skipTests>--> <skipTests>false</skipTests> <suiteXmlFiles> <suiteXmlFile>suites/test.xml</suiteXmlFile> </suiteXmlFiles> <properties> <property> <name>usedefaultlisteners</name> <value>false</value> </property> <property> <name>listener</name> <value>org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter</value> </property> </properties> <forkMode>always</forkMode> </configuration> <!--jar--> <dependencies> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.8.10</version> </dependency> </dependencies> </plugin> <!--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> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-shade-plugin</artifactId> <version>3.0.0</version> <configuration> <!-- put your configurations here --> <transformers> <transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> <manifestEntries> <Main-Class>org.testng.TestNG</Main-Class> </manifestEntries> </transformer> </transformers> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>shade</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
2.在Terminal执行:mvn clean test -Dsurefire.suiteXmlFiles=suites/test.xml,即可执行成功。 或者直接执行 mvn test【执行的suites/test.xml已经通过上述的pom.xml配置过了,所以可以直接执行】
3.执行命令其实来源于maven helper依赖: