0

I have a set of test suites defined in testng.xml file and I am passing an argument from pom.xml file to call the testng file

However I need a solution where I can pass an argument as environment variable to decide which suite I want to execute from testng.xml

My initial thoughts were to have multiple testng.xml files but having multiple files doesnt seem to be the best solution

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
    <test name="Sanity">
        <classes>
            <class name="com.ibm.wce.scbn.cc.runner.Sanity" />
        </classes>
    </test> 
</suite> 
<suite name="Suite">
    <test name="Regression">
        <classes>
            <class name="com.ibm.wce.scbn.cc.runner.Reg" />
        </classes>
    </test> 
</suite> 

Pom.xml

<plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M3</version>
                <configuration>
                    <!-- TestNG Suite XML files list for test execution -->
                    <suiteXmlFiles>
                        <suiteXmlFile>${suiteXmlFile}</suiteXmlFile>
                    </suiteXmlFiles>
                </configuration>
            </plugin>

VM Argument : mvn clean install -DsuiteXmlFile=testng.xml,testng2.xml

Suraj Prasad
  • 241
  • 3
  • 24

1 Answers1

0

I think you are looking for profiles in maven . Look here for more info

In your pom.xml , add profiles section and your plugins go into <pluginManagement> within build tag like below

  <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  <build>
    <sourceDirectory>src</sourceDirectory>
    <resources>
      <resource>
        <directory>src</directory>
        <excludes>
          <exclude>**/*.java</exclude>
        </excludes>
      </resource>
    </resources>

 <pluginManagement> 


     <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-surefire-plugin</artifactId>
             <version>2.22.1</version>

            <configuration>
                          <suiteXmlFiles>
                          <suiteXmlFile>suite1.xml</suiteXmlFile>
                          </suiteXmlFiles>

            </configuration>
     </plugin>
   </plugins>

 </pluginManagement>  

   <plugins>
         <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         </plugin>

   </plugins>

  </build>



  <profiles> 
        <profile>

            <id>suite1</id>

            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>

                         <configuration>
                                  <suiteXmlFiles>
                                  <suiteXmlFile>testng-customsuite.xml</suiteXmlFile>
                                  </suiteXmlFiles>

                         </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>

         <profile>

            <id>suite2</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>

                         <configuration>
                                  <suiteXmlFiles>
                                  <suiteXmlFile>suite2.xml</suiteXmlFile>
                                  </suiteXmlFiles>

                         </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

<dependencies> 
    ....
 </dependencies>

Then based on profiles you can activate a profile like this from command line and then suite mentioned in that profile gets executed

mvn clean test -P suite2

Note that

<pluginManagement> - define plugins

<plugins> - activate plugins

<profile> - run suitable plugin based on what you pass in command line

These profile can also be activated through environment variables

From above link Supported variables are system properties like ${user.home} and environment variables like ${env.HOME}

EDIT: Have a look at this , This seems to suggest what you are trying to do may not be possible but you can try second solution . That is what I meant when I copy pasted above text from official site.

user1207289
  • 3,060
  • 6
  • 30
  • 66
  • Thanks for the information .The above method requires multiple testng xml files. However my requirement is that I have suite 1 ,2 ,3, 4 defined in testng.xml. On run time I want to select suite1 or 2 or both together – Suraj Prasad Oct 03 '19 at 07:06
  • What is the issue defining separate xml files ? You can still run those suites from one main suite file – user1207289 Oct 04 '19 at 15:11