一、先在pom的<properties>标签内添加env参数,在@Test方法下添加
System.out.println(System.getProperty("env"));
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>15</maven.compiler.source> <maven.compiler.target>15</maven.compiler.target> <env>test</env> </properties>
由此得知,System.getProperty方法并不能获取properties标签中值。
二、注释掉properties标签的新增的变量,在maven-surefire-plugin下的<configuration>标签添加env,并执行
<systemPropertyVariables> <env>pre</env> </systemPropertyVariables>
结果:控制台输出了pre,由此得知,getProperty方法能够获取到systemPropertyVariables的值。
三、运行参数中添加env
mvn clean test -Dsurefire.suiteXmlFiles=src/test/java/org/example/suite/testng.xml -Denv=pre2
结果:如果env变量名存在于systemPropertyVariables中,则会输出pre2,如果命令行中参数名不是env,且systemPropertyVariables中存在env,
则输出systemPropertyVariables中的env值。
四、properties标签中设置env,同时在systemPropertyVariables中引用
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>15</maven.compiler.source> <maven.compiler.target>15</maven.compiler.target> <env>test</env> </properties>
<systemPropertyVariables> <env>${env}</env> </systemPropertyVariables>
结果:正确输出env的值
综述:
a.properties标签中定义的变量只供systemPropertyVariables标签中引用,且两处的标签要一致
b.命令行上追加的参数,实际上是直接改变systemPropertyVariables中的值
c.jenkins中定义的String Parameters,实际上在shell构建中追加到mvn命令行的,即是直接给systemPropertyVariables中的参数传参
d.properties中相当于systemPropertyVariables中的初始化值,而传参,相当于重新赋值。
那suiteXmlFile标签中是否可以引用在properties标签中定义的默认值呢?
1、在properties中增加suite标签,值是testng.xml
2、在suiteXmlFile中引用,提交到gitlab,构建jenkins
结果:执行成功
结语:
1、Jenkins中新建String parameters可以通过${name}或者$name方式引用,name是变量名
2、参数构建场景可以用于接口地址和环境切换上,在properties和systemPropertyVariables设定默认接口地址,
代码中通过System.getProperty()方法来判断是否切换接口。当然也可以在代码中配置不同的地址,通过命令行
传递自定义的变量判断是否切换接口地址