简体   繁体   中英

How do I tell Maven and TestNG to run either a specific test class or a suite.xml file?

I am using Maven to run TestNG integration tests against a website. Currently, I can use this command to run a specific test class:

mvn -Dit.test="MyTestClassname" verify

I would also like to be able to specify a TestNG suite XML file to run a bunch of classes together with specific parameters.

I can set up the Failsafe plugin in my POM to accept a suiteXmlFiles parameter like so:

<configuration>
                        <threadCount>1</threadCount>
                        <suiteXmlFiles>
                            <suiteXmlFile>${basedir}/src/test/resources/suites/${suiteFile}</suiteXmlFile>
                        </suiteXmlFiles>
                        <systemPropertyVariables>
                            <browser>${browser}</browser>
                            <environment>${environment}</environment>
                            <debugMode>${debugMode}</debugMode>
                            <caseId>${caseId}</caseId>
                        </systemPropertyVariables>

</configuration>

That lets me issue this command:

mvn -DsuiteFile="MyTestSuite.xml"

But now I can't use the -Dit.test="MyTestClassname" option, because Maven is now trying to pass an empty value as the suite name.

Is there a way around this? Again, I want to either:

  • Specify a single test class (with a bunch of parameters at the command line)
  • Specify a testng XML file containing a bunch of test classes and different parameters.

I am using maven-surefire-plugin. It lets me run both suitefile and a class. This is my surefire plugin

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.17</version>
                <configuration>
                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>
                    <skipTests>${skipTests}</skipTests>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>

To run suitefile , I use

mvn test -DsuiteXmlFile=<suitefile>

And to run a class file, I use

mvn test -Dtest=<qualified path of the test class>

The above method did not work for me. If you're also getting an error on your pom.xml when you pass ${suiteXmlFile} or other issues as I was having:

  <suiteXmlFiles>
     <suiteXmlFile>*ERROR*${suiteXmlFile}*ERROR*</suiteXmlFile>
  </suiteXmlFiles>

Here is a better solution, at the command, cd to the location of your pom.xml file thenn run on command line:

mvn test -Dsurefire.suiteXmlFiles=yourfileName(WithFullPathIfNotInRootDirectory).xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM