Search code examples
testng

Overriding testNG.xml suite parameter with Jenkin parameter value through Maven goals


Trying to override testNG.xml suite parameter with Jenkins parameter value. But values are not getting replaced. Want to replace the testNG parameters with Jenkin parameter. Can someone please guide. Version used TestNG '7.5' and Open JDK '15'

Maven goals : clean compile test -DtestNGXml=${testNGXml} -DenvironmentName=${environmentName} -DenvironmentClientID=${environmentClientID}

TestNG.xml

<suite name="HealthCheck_Suite" parallel="classes" thread-count=“1”>
    <parameter name="environmentName" value="DEV" />
    <parameter name="environmentClientID" value="BE11TEST" />
    <test name="iOS_HealthCheck">
        <classes>
            <class name="MobileLoginTest">
                <methods>
                    <include name="loginHealthCheckScript" />
                </methods>
            </class>            
        </classes>
    </test>
</suite>

Pom Xml surefire plugin:

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
      <argLine>-javaagent:"${settings.localRepository}/org/aspectj/aspectjweaver/${aspectj.version}/aspectjweaver-${aspectj.version}.jar"</argLine>
      <suiteXmlFiles>
         <suiteXmlFile>${testNGXml}</suiteXmlFile>
      </suiteXmlFiles>
   </configuration>
   <dependencies>
      <dependency>
         <groupId>org.aspectj</groupId>
         <artifactId>aspectjweaver</artifactId>
         <version>${aspectj.version}</version>
         <scope>runtime</scope>
      </dependency>
   </dependencies>
</plugin>```

Solution

  • Here's a simplified setup that should work for you.

    Lets assume that your test looks like below:

    package com.rationaleemotions;
    
    import org.testng.annotations.Parameters;
    import org.testng.annotations.Test;
    
    public class AppTest {
    
        @Test
        @Parameters({"environmentName", "environmentClientID"})
        public void testMethod(String environmentName, String environmentClientID) {
            System.err.println("EnvironmentName: " +
                environmentName + ", environmentClientID: " + environmentClientID);
        }
    }
    

    A suite xml would look like below:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
    <suite name="jenkins_Suite" parallel="classes" thread-count="1">
      <parameter name="environmentName" value="DEV"/>
      <parameter name="environmentClientID" value="BE11TEST"/>
      <test name="jenkins_test">
        <classes>
          <class name="com.rationaleemotions.AppTest"/>
        </classes>
      </test>
    </suite>
    

    I have a TestNG dependency of 7.6.1 and my surefire-plugin configuration looks like below:

    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0-M5</version>
      <configuration>
        <suiteXmlFiles>
          <suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
        </suiteXmlFiles>
      </configuration>
    </plugin>
    

    I now create a parameterised job, that basically has 2 string parameters:

    1. env - This represents the environment
    2. clientId - This represents the client id

    The maven goal would look like below

    clean test -DenvironmentName="${env}" -DenvironmentClientID="${clientId}"
    

    This should now let you override the parameter values defined in your suite xml file, via JVM arguments.

    This is possible because TestNG already lets you override values in <parameter> tags via JVM arguments.

    So:

    • <parameter name="environmentName" value="DEV"/> - Can be overridden via -DenvironmentName
    • <parameter name="environmentClientID" value="BE11TEST"/> - Can be overridden via -DenvironmentClientID