mvn test 传递系统变量给TestNG
废话一下。因为五一我们国内BPM要迁移数据库,所以需要留一部分测试来协助运维,在迁移完成后点检整个项目,我很自觉地报名(安排)了
然后之前刚好写了一个点检所有模块文档的功能,这次又可以用上了。但是单个环境执行的话需要的时间太久了,所以在想能不能根据每个领域来点检(我们是每个人负责几个领域),如果自己点检自己的领域,然后哪个模块点检失败自己去看就效率高很多了
一开始是想着把领域写死,对应的人写对应的领域执行对应的程序包,随即一想不行太low了便作罢,于是便搜索一下,发现还真有解决办法,下面上我的实施过程(这是原帖地址:https://www.cnblogs.com/wade-xu/p/4857471.html)
1,我是直接在plugin里加了一个标签systemPropertyVariables,在里面加上自定义的系统变量标签moduleNumber
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.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>3.0.0-M3</version>
<configuration>
<forkMode>once</forkMode>
<argLine>-Dfile.encoding=UTF-8</argLine>
<encoding>UTF-8</encoding>
<suiteXmlFiles>
<!--你的testng.xml文件路径-->
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
<systemPropertyVariables>
<moduleNumber>${bpm.autotest.moduleNumber}</moduleNumber>
</systemPropertyVariables>
<testFailureIgnore>true</testFailureIgnore>
</configuration>
</plugin>
</plugins>
</build>
2,然后定义profile,给自定义moduleNumber赋值,我们领域定位的方式刚好可以用nth:child(index)来定位,所以对应的领域写好对应的index即可
<profiles>
<profile>
<id>所有领域</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<bpm.autotest.moduleNumber>0</bpm.autotest.moduleNumber>
</properties>
</profile>
<profile>
<id>公司公共</id>
<properties>
<bpm.autotest.moduleNumber>1</bpm.autotest.moduleNumber>
</properties>
</profile>
</profiles>
2.1,我这里配置了一个默认值,如果啥参数没传,就表示点检所有领域,代码里加个判断
3,然后参数传递进来后,代码需要修改,不能用之前的遍历所有领域,而是根据领域编号来(这里需要提到一个点,单个领域的话先点击哪个领域,那个领域的编号就是几,所以需要按照顺序先把所有领域按照遍历顺序点一遍,加载好index,不然就会乱)
4,再就是在BaseTest里加一个初始化动作,把领域跟对应的index存到集合中
5,然后在测试人员传递对应的领域名称后,通过System.getProperty("moduleNumber")就可以拿到对应的index来定位领域了
输入dos命令:mvn test -P 公司公共