【问题标题】:How to pass java code a parameter from maven for testing如何从maven传递java代码一个参数进行测试
【发布时间】:2012-10-26 01:46:44
【问题描述】:

我需要传递以下值……

exeEvironment(测试环境), testGroup(testNG 中的组)

从命令行 -> POM -> TestNG -> 测试用例。

基于这两个帖子....

pass a java parameter from maven

How to pass parameters to guicified TestNG test from Surefire Maven plugin?

我做了如下配置..

surefire 插件 中,我尝试了以下两个选项,但似乎都不起作用。

=====

(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>

testNG.xml 中,我可以使用变量testGroup 像...

<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)

testEnv = System.getProperty("exeEnvironment");
testGroup = System.getProperty("testGroup");

【问题讨论】:

    标签: maven command-line testng pom.xml surefire


    【解决方案1】:

    这正是我正在寻找自动化测试的东西,并且我得到了它的工作。

    命令行参数

    mvn clean test -Denv.USER=UAT -Dgroups=Sniff
    

    我的 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 测试

    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);
        }
    }
    

    上面的效果很好。另外,如果需要使用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>
    

    另外,我更喜欢在 @Test() 中使用 @Parameters 而不是 parameters,因为后者已被弃用。

    【讨论】:

    • 看起来很有希望,让我试试再回来。感谢分享。
    • 对了,你在这里用testng.xml吃了吗?
    • 我没有使用testng.xml,顺便说一下你不需要在POM文件中添加参数。 mvn clean test -Denvironment=QA -Dgroups=Regression if you use this 1. 只有回归组会被执行 (@Test(groups = { "Sniff", "Regression" }) 2. @Parameters("environment") - value “QA”将直接传递给您的测试
    • 运行顺利,顺便说一句,您需要使用 mvn clean test "-D..​​." 执行
    • 如果您想传递多个参数,可以用逗号分隔它们,例如@Parameters({"param1","param2"})
    【解决方案2】:

    您不需要在 testng xml 或 pom 中为组定义任何内容,支持是内置的。您可以简单地在 cmd 行上指定组 http://maven.apache.org/plugins/maven-surefire-plugin/test-mojo.html#groups

    希望对你有帮助..

    编辑 2:

    好的..所以这是另一个选项...实现IMethodInterceptor

    定义您的自定义属性。 在命令行调用中使用 -Dcustomproperty=groupthatneedstoberun。

    在拦截调用中,扫描所有方法..有什么效果..

    System.getProperty("customproperty");
    for(IMethodInstance ins : methods) {
        if(ins.getMethod().getGroups()) contains group)
            Add to returnedVal;
        }
    return returnedVal;
    

    将此添加到您的 xml 中的侦听器列表中。

    【讨论】:

    • 感谢您的回答,您是正确的,组是在surefire pom 插件中内置的,但如果我指定我指定的suiteXMLFile,它将被忽略。所以基本上我想在命令行上动态地向 POM 提供组,它应该能够使用 testNG 运行这些测试。请问还有其他的想法吗?
    【解决方案3】:

    完美。

    将变量从 POM.xml 传递到 ABC.java 的最简单方法

    POM.xml

    <properties>
       <hostName>myhostname.com</hostName>
    </properties>
    

    ABC.java 中,我们可以像这样从系统属性中调用它

    System.getProperty("hostName")
    

    【讨论】:

    • 16:20:15,676 DEBUG TestApp:21 - hostname: null
    • 这对我来说也失败了一个“null”
    【解决方案4】:

    传递浏览器等参数可以如下完成:

    <properties>    
        <BrowserName></BrowserName>
        <TestRunID></TestRunID>
    </properties>
    
    
    
            <!-- Below plug-in is used to execute tests -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.18.1</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>src/test/resources/${testXml}</suiteXmlFile>
                    </suiteXmlFiles>
                    <systemPropertyVariables>
                      <browserName>${BrowserName}</browserName>
                        <testRunID>${TestRunID}</testRunID> 
                    </systemPropertyVariables>
                </configuration>
                <executions>
                    <execution>
                        <id>surefire-it</id>
                        <phase>integration-test</phase>
                        <goals>
                            <goal>test</goal>
                        </goals>
                        <configuration>
                            <skip>false</skip>
                            <testFailureIgnore>true</testFailureIgnore>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
    

    并在 java 代码中处理这个使用这个:

     public static final String Browser_Jenkin=System.getProperty("BrowserName");
        public static final String TestRunID=System.getProperty("TestRunID");
    
      public static String browser_Setter()
        {
            String value=null;
            try {
                if(!Browser_Jenkin.isEmpty())
                {
                    value = Browser_Jenkin;
                }
            } catch (Exception e) {
                value =propObj.getProperty("BROWSER");
            }
            return value;   
        }
    
        public static String testRunID_Setter()
        {
            String value=null;
            try {
                if(!TestRunID.isEmpty())
                {
                    value = TestRunID;
                }
            } catch (Exception e) {
                value =propObj.getProperty("TEST_RUN_ID");
            }
            return value;   
        }
    

    【讨论】:

      【解决方案5】:

      您无需使用环境变量或编辑 pom.xml 即可使用它们。

      Build 部分下 Invoke Maven 3 的目标和选项采用参数。试试这个(假设你参数化了构建):

      Invoke Maven 3
        Goals and options = test -Denv=$PARAM_ENV -Dgroup=$PARAM_GROUP
      

      【讨论】:

        【解决方案6】:

        基于accepted answer

        如果 maven surefire 和 &lt;systemPropertyVariables&gt; 在 maven 配置文件中声明,则它们不可用并且将返回 null,除非还调用了配置文件。

        命令行参数

        mvn clean test -PmyTestProfile -Denv.USER=UAT -Dgroups=Sniff 
        

        pom.xml

        <profiles>
            <profile>
                <id>myTestProfile</id>
                <activation>
                    <activeByDefault>false</activeByDefault>
                </activation>
        
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>${surefire.version}</version>
                            <configuration>
                                <systemPropertyVariables>
                                    <environment>${env.USER}</environment>
                                </systemPropertyVariables>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>
        </profiles>
        

        【讨论】:

          猜你喜欢
          • 2022-11-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-04-23
          • 1970-01-01
          • 2021-06-12
          • 2023-03-06
          相关资源
          最近更新 更多