天天看点

利用maven插件maven-surefire-plugins指定执行testNG.xml并传递参数pom.xml添加插件maven-surefire-plugins设置参数,testNG传递参数testNG通过@Parameters({ “phone”, “pwd” })获取参数

maven插件maven-surefire-plugins

  • pom.xml添加插件maven-surefire-plugins
  • 设置参数,testNG传递参数
  • testNG通过@Parameters({ "phone", "pwd" })获取参数

pom.xml添加插件maven-surefire-plugins

pom.xml层级,suiteXmlFiles设置指定的testNG.xml文件

<build>
	<pluginManagement>
		<plugins>
			<!-- 添加maven-surefire-plugins插件-->
			<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.18.1</version>
					<configuration>
						<suiteXmlFiles>
							<!--此处testng.xml即为要运行的testng.xml文件 -->
							<suiteXmlFile>testng.xml</suiteXmlFile>
						</suiteXmlFiles>
					</configuration>
				</plugin>
		</plugins>
	</pluginManagement>
</build>
           

设置参数,testNG传递参数

pom.xml层级,systemPropertyVariables设置参数,<参数名>参数值</参数名>,通过propertyName传递参数

<build>
	<pluginManagement>
		<plugins>
			<!-- 添加maven-surefire-plugins插件-->
			<plugin>
					<groupId>org.apache.maven.plugins</groupId>
					<artifactId>maven-surefire-plugin</artifactId>
					<version>2.18.1</version>
					<configuration>
						<!-- 设置参数-->
						<systemPropertyVariables>
						    <phone>123456789</phone>
						    <pwd>abcd1234</pwd>
						    <propertyName>${phone}</propertyName>
						    <propertyName>${pwd}</propertyName>
						</systemPropertyVariables>
						<suiteXmlFiles>
							<!--此处testng.xml即为要运行的testng.xml文件 -->
							<suiteXmlFile>testng.xml</suiteXmlFile>
						</suiteXmlFiles>
					</configuration>
				</plugin>
		</plugins>
	</pluginManagement>
</build>
           

testNG通过@Parameters({ “phone”, “pwd” })获取参数

@Test
	@Parameters({ "phone", "pwd" })
	public void test(String phone, String pwd) {
		System.out.println("phone:"+phone);
		System.out.println("pwd:"+pwd);
	}
           

执行pom.xml结果:

phone:123456789
pwd:abcd1234