How to pass parameter to maven test

倖福魔咒の 提交于 2019-12-20 02:58:05

问题


I have One test suite running in two environment. Sometimes, I would like to run tests in localhost:8080 and sometimes at localhost:8585. Jenkins run the tests by "mvn test" command.

How could I pass the port by parameter? Something like "mvn test 8080".


回答1:


I add a plugin on 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>

And get the parameter in junit code with

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

After, I run my tests with -DfileName argument

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

Now, I can put all configurations in xml file and load appropriate file with the corrects parameters.

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

or

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

I solved the problem with the tips from Sandra Sukarieh and http://syntx.io/how-to-pass-parameters-to-the-junit-tests-from-the-maven-surefire-plugin/

Thank you very much




回答2:


try this:

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

and portValue will be either 8080 or 8585, and while you have a "port" variable declared in your test code.




回答3:


After doing some research, I found the below code:

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

Please run the maven command:

mvn test -Dtestsuite =yourxmlsuitepath


来源:https://stackoverflow.com/questions/32120909/how-to-pass-parameter-to-maven-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!