John Chesshir
John Chesshir

Reputation: 640

How to override TestNG parameters from the calling program

I am using TestNG 6.0 with Maven Surefire 2.18. I am trying to use a default value for a parameter in my testng.xml file and override it in my Maven pom.xml, which references the testng.xml file for its default values. Here is a sample of my code:

class TestClass {
    @Parameters({ "parm1" })
    @BeforeClass
    void setup(String parm1) {
        System.out.println("parm1 = " + parm1);
    }
}

Here is my testng.xml file:

<suite name="suite1">
    <parameter name="parm1" value="Default Value">
    <test name="myTest">
        <classes>
            <class name="TestClass"/>
        </classes>
    </test>
</suite>

And here is the surefire plugin entry in my pom.xml file:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
        </suiteXmlFiles>
        <systemPropertyVariables>
            <parm1>Override Value</parm1>
        </systemPropertyVariables>
    </configuration>
</plugin>

Upvotes: 3

Views: 1535

Answers (1)

John Chesshir
John Chesshir

Reputation: 640

I figured out what is going on here. It turns out that the testng.xml, when used, reigns supreme. It's a little backwards IMHO, but that's how it is.

To get around the issue, I made the field optional and specified the default value in my java code...

class TestClass {
    @Parameters({ "parm1" })
    @BeforeClass
    void setup(@Optional("Default Value") String parm1) {
        System.out.println("parm1 = " + parm1);
    }
}

...while eliminating the parameter from the testng.xml file.

<suite name="suite1">
    <test name="myTest">
        <classes>
            <class name="TestClass"/>
        </classes>
    </test>
</suite>

This freed up the TestNG engine to look for the parameter value in the system properties, where the parameter is already added in the pom.xml shown in the question.

Upvotes: 1

Related Questions