如何将参数传递给maven测试

时间:2015-08-20 14:25:05

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

我有一个在两个环境中运行的测试套件。 有时,我想在localhost:8080中运行测试,有时在localhost:8585上运行。 詹金斯通过" mvn test"进行测试。命令。

我怎么能通过参数传递端口?类似于" mvn test 8080"。

3 个答案:

答案 0 :(得分:4)

我在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文件中,并使用corrects参数加载适当的文件。

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/

的提示解决了这个问题

非常感谢

答案 1 :(得分:1)

试试这个:

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

和portValue将是8080或8585,并且您在测试代码中声明了“port”变量。

答案 2 :(得分:0)

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

<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