如何传递java代码的一个参数从maven进行测试 [英] How to pass java code a parameter from maven for testing
问题描述
我需要传递以下值...
exeEvironment (测试环境),
testGroup POM - > TestNG - >测试用例>>(Group in testNG)
基于这两个帖子....
如何将参数传递给来自Surefire Maven插件的Guicified TestNG测试? / a>
我做了以下配置..
,我试过以下两个选项,似乎没有工作。
=====
(1)
< execution>
< id> default-test< / id>
< goals>
< goal> test< / goal>
< / goal>
< configuration>
< properties>
< exeEnvironment> $ {exeEnvironment}< / exeEnvironment>
< testGroup> $ {testGroup}< / testGroup>
< / properties>
< suiteXmlFiles>
< suiteXmlFile> testng.xml< / suiteXmlFile>
< / suiteXmlFiles>
< / configuration>
< / execution>
(2)
< execution>
< id> default-test< / id>
< goals>
< goal> test< / goal>
< / goal>
< configuration>
< systemPropertyVariables> < exeEnvironment> $ {exeEnvironment}< / exeEnvironment>
< testGroup> $ {testGroup}< / testGroup> < / systemPropertyVariables>
< suiteXmlFiles>
< suiteXmlFile> testng.xml< / suiteXmlFile>
< / suiteXmlFiles>
< / configuration>
< / execution>
在 testNG.xml ,我可以使用变量 testGroup
like ...
< test name =Web Build Acceptance>
< groups>
< run>
< include name =$ {testGroup} />
< / run>
< / groups>
< classes>
< class name =com.abc.pqr/>
< / classes>
< / test>
这似乎不行,我需要定义一个参数。
在测试用例中,我尝试通过以下两种方式获取变量:。
(1)
testEnv = testContext.getSuite()。getParameter(exeEnvironment);
testGroup = testContext.getSuite()。getParameter(testGroup);
(2)
>testGroup =
testEnv = System.getProperty(exeEnvironment);
testGroup = System.getProperty / pre>
解决方案这是我正在寻找的自动化测试
命令行参数
mvn clean test -Denv.USER = UAT -Dgroups = Sniff
我的Pom Xml
<?xml version =1.0encoding =UTF-8?&
< project xmlns =http://maven.apache.org/POM/4.0.0
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instance
xsi:schemaLocation =http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd\">
< modelVersion> 4.0.0< / modelVersion>
< groupId> TestNg< / groupId>
artifactId> TestNg< / artifactId>
< version> 1.0< / version>
< dependencies>
< dependency>
< groupId> org.testng< / groupId>
< artifactId> testng< / artifactId>
< version> 6.8< / version>
< scope> test< / scope>
< / dependency>
< / dependencies>
< build>
< plugins>
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
< version> 2.12.4< / version>
< configuration>
< systemPropertyVariables>
< environment> $ {env.USER}< / environment>
< / systemPropertyVariables>
< / configuration>
< / plugin>
< / plugins>
< / build>
< / project>
TestNG测试
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestAuthentication {
@Test(groups = {Sniff,Regression})
public void validAuthenticationTest b $ b System.out.println(Sniff + Regression+ System.getProperty(environment));
}
@Test(groups = {Regression},parameters = {environment})
public void failedAuthenticationTest(String environment){
System。 out.println(Regression - + environment);
}
@Parameters(environment)
@Test(groups = {Sniff})
public void newUserAuthenticationTest(String environment){
System.out.println(Sniff - + environment);
}
}
以上工作良好。此外,如果您需要使用 testng.xml
,您可以指定 suiteXmlFile
like ...
< plugin>
< groupId> org.apache.maven.plugins< / groupId>
< artifactId> maven-surefire-plugin< / artifactId>
< version> 2.12.4< / version>
< configuration>
< systemPropertyVariables>
< environment> $ {env.USER}< / environment>
< / systemPropertyVariables>
< suiteXmlFiles>
< suiteXmlFile> testng.xml< / suiteXmlFile>
< / suiteXmlFiles>
< / configuration>
< / plugin>
此外,我更喜欢使用 @Parameters
$ 中的参数,因为后面的版本已弃用。
I need to pass on following values …
exeEvironment (Test environment) ,
testGroup (Group in testNG)
from Command-Line -> POM -> TestNG -> Test cases.
Based on these two posts ....
pass a java parameter from maven
How to pass parameters to guicified TestNG test from Surefire Maven plugin?
I did the following configuration ..
In surefire plugin, I tried following two options, none seem to work.
=====
(1)
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<properties>
<exeEnvironment>${exeEnvironment}</exeEnvironment>
<testGroup>${testGroup}</testGroup>
</properties>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</execution>
(2)
<execution>
<id>default-test</id>
<goals>
<goal>test</goal>
</goals>
<configuration>
<systemPropertyVariables> <exeEnvironment>${exeEnvironment}</exeEnvironment>
<testGroup>${testGroup}</testGroup> </systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</execution>
In testNG.xml , can I use the the variable testGroup
like …
<test name="Web Build Acceptance">
<groups>
<run>
<include name="${testGroup} />
</run>
</groups>
<classes>
<class name="com.abc.pqr" />
</classes>
</test>
This doesn't seem to work as well, do I need to define a parameter.
In the test cases , I tried to get he variables in following two ways ….
(1)
testEnv = testContext.getSuite().getParameter("exeEnvironment");
testGroup = testContext.getSuite().getParameter("testGroup");
(2)
testEnv = System.getProperty("exeEnvironment");
testGroup = System.getProperty("testGroup");
解决方案 This is the exact thing I was looking for my automation test and I got it working.
Command Line argument
mvn clean test -Denv.USER=UAT -Dgroups=Sniff
My Pom Xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>TestNg</groupId>
<artifactId>TestNg</artifactId>
<version>1.0</version>
<dependencies>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<systemPropertyVariables>
<environment>${env.USER}</environment>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
TestNG test
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestAuthentication {
@Test (groups = { "Sniff", "Regression" })
public void validAuthenticationTest(){
System.out.println(" Sniff + Regression" + System.getProperty("environment"));
}
@Test (groups = { "Regression" },parameters = {"environment"})
public void failedAuthenticationTest(String environment){
System.out.println("Regression-"+environment);
}
@Parameters("environment")
@Test (groups = { "Sniff"})
public void newUserAuthenticationTest(String environment){
System.out.println("Sniff-"+environment);
}
}
The above works well. Additionally, if you need to use testng.xml
, you can specify the suiteXmlFile
like ...
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.12.4</version>
<configuration>
<systemPropertyVariables>
<environment>${env.USER}</environment>
</systemPropertyVariables>
<suiteXmlFiles>
<suiteXmlFile>testng.xml</suiteXmlFile>
</suiteXmlFiles>
</configuration>
</plugin>
Also, I prefer using @Parameters
instead of parameters
in @Test()
as the later is deprecated.
这篇关于如何传递java代码的一个参数从maven进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!