天天看點

利用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