Running different test suites using testng and maven

23,122

Solution 1

TestNG supports groups of tests, either by specifying groups on test classes/methods in the test cases themselves, or in the suite.xml file. By using groups, you can put all your tests in one xml file. See Groups in the TestNG user guide.

The surefire plugin allows tests to be included or excluded based on group:

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.8.1</version>
        <configuration>
          <groups>${testng.groups}</groups>
        </configuration>
      </plugin>

You can put all your tests in one xml file, and then choose which ones to run by setting the group or groups to include in the ${testng.groups} property, which should be a comma-delimited list of group names.

You can define value for the ${testng.groups} property in the POM using profiles, or on the command-line -Dtestng.groups=[groups to run].

Solution 2

What you can do is to define different profiles

  <profiles>
    <profile>
      <id>t1</id>
      <build>
        <plugins>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.8.1</version>
          <configuration>
            <suiteXmlFiles>
              <suiteXmlFile>testng.xml</suiteXmlFile>
            </suiteXmlFiles>
          </configuration>
        </plugin>
      </plugins>
      </build>
     </profile>
   </profiles>

and call from command line via mvn -Pt1 ... or define a property in the profile and use that property in the configuraiton.

Solution 3

Also note that TestNG allows you to combine multiple suites into one, e.g. if you wanted to combine your api and ui smoke tests into one suite:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="uber-smoke-suite" verbose="1" parallel="methods" thread-count="1" configfailurepolicy="continue">
  <suite-files>
    <suite-file path="smoke_api.xml" />
    <suite-file path="smoke_ui.xml" />
  </suite-files>
</suite>

That way you can also create an uber suite too that combines all your tests into one, but still allows you to run single suite if necessary, e.g.:

-Dtestfile=smoke
-Dtestfile=smoke_api
-Dtestfile=smoke_ui
Share:
23,122
ekaqu
Author by

ekaqu

Updated on July 20, 2022

Comments

  • ekaqu
    ekaqu almost 2 years

    I am using TestNg and Maven with the surefire plugin to run my tests. I have several different components that I want to be able to run at different times using the same pom. Currently to do this I have several different XML files defining a test suite and I have the pom set up so i can do mvn test -Dtestfile=/path and use that suite instead.

    I was wondering if there is a way to combine the XML files into one file and chose base off testnames or some other system?

    EDIT: I have all my tests defined with Smoke, Sanity, Regression already and I would like to be able to run all Regressions for a given component. If I run through the TestNG CLI, I am able to give -testnames comp1,comp2,comp3,etc. where each component is defined in one xml suite which contains multiple tests (). I was wondering if there was any way to do this in maven short of using the exec:java plugin.

  • 12rad
    12rad almost 12 years
    I tried doing this. So, did you set different testng.xml files for different profiles?