【问题标题】:How to pass parameter to maven test如何将参数传递给maven测试
【发布时间】:2015-08-20 14:25:04
【问题描述】:

我有一个测试套件在两个环境中运行。 有时,我想在 localhost:8080 中运行测试,有时在 localhost:8585 中运行测试。 Jenkins 通过“mvn test”命令运行测试。

如何通过参数传递端口?类似“mvn test 8080”。

【问题讨论】:

  • 我忘记了一个细节。我必须在我的 junit 代码中使用端口:driver.get("localhost:8080");

标签: maven unit-testing testing junit automated-tests


【解决方案1】:

我在 maven pom.xml 上添加了一个插件

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-surefire-plugin</artifactId>
     <configuration>
        <systemProperties>
          <property>
             <name>fileName</name>
             <value>${fileName}</value>
          </property>
        </systemProperties>
     </configuration>
</plugin>

并在junit代码中获取参数

String fileName = System.getProperty("fileName");

之后,我使用 -DfileName 参数运行测试

mvn clean test -DfileName="config-test.xml"

现在,我可以将所有配置放在 xml 文件中,并使用正确的参数加载适当的文件。

mvn clean test -DfileName="config-test.xml"

mvn clean test -DfileName="config-homolog.xml"

我通过 Sandra Sukarieh 和 http://syntx.io/how-to-pass-parameters-to-the-junit-tests-from-the-maven-surefire-plugin/ 的提示解决了这个问题

非常感谢

【讨论】:

    【解决方案2】:

    试试这个:

    mvn -Dtest=testName -Dargline="-Dport=portValue"
    

    portValue 将是 8080 或 8585,而您在测试代码中声明了一个“端口”变量。

    【讨论】:

    • 我有一个变量“公共字符串端口;”我执行“mvn test -Dtest=RegrasAutoriaLBTest -Dargline="-Dport=8080"。但是 System.out.println(port) = null
    • 尝试将端口设为整数而不是字符串
    • @SandraSukarieh ,我正在使用您的字符串类型参数的方法。但它没有成功地将参数传递给我的测试类。
    【解决方案3】:

    经过一番研究,我发现了以下代码:

    <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
            <systemProperties>
              <property>
                <suiteXmlFiles>
                <suiteXmlFile>${testsuite}</suiteXmlFile>
                </suiteXmlFiles>
              </property>
            </systemProperties>
         </configuration>
    </plugin>
    

    请运行maven命令:

    mvn test -Dtestsuite =yourxmlsuitepath
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-25
      • 2015-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-10
      • 1970-01-01
      相关资源
      最近更新 更多