0

The following post, several years old, outlines that it is possible to specify method level parameters in a testng suite xml:

For example - where an ID needs to be passed to the method, which will be unique (per configuration):

<methods>
        <include name="testX">
          <parameter name="ID" value ="3452"/>
        </include>
        <include name="testY">
           <parameter name="ID" value ="3453"/>
        </include>
</methods>

Is this still possible? I don't see it mentioned in the testng documentation. (I need to include a particular parameter at the method level, not the test level)

If this is possible, how can I obtain the value of the method parameter, as it does not seem to be picked up with @Parameters inside the test case class file.

The referenced post does refer to using:

iTestResult.getMethod().findMethodParameters(iTestContext.getCurrentXmlTest())

But if this is still applicable, some guidance on placing it in the correct listener method would be appreciated.

Thanks

David
  • 197
  • 3
  • 19

1 Answers1

1

This is very much possible and still supported in TestNG. Here's a complete sample that shows all of this in action.

I am using TestNG 6.13.1 for this (This is the latest released version of TestNG as of today)

Sample test class

package com.rationaleemotions.stackoverflow.qn48171506;

import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class SampleTestClass {
    @Test
    @Parameters(value = "name")
    public void helloA(String name) {
        System.err.println("helloA() says " + name);
    }
    @Test
    @Parameters(value = "anotherName")
    public void helloB(String name) {
        System.err.println("helloB() says " + name);
    }
    @Test
    @Parameters(value = "someOtherName")
    public void helloC(String name) {
        System.err.println("helloC() says " + name);
    }
}

Sample test listener

package com.rationaleemotions.stackoverflow.qn48171506;

import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;

public class TestListener implements IInvokedMethodListener {
    @Override
    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
        showMessage("About to run ", method, testResult);
    }

    @Override
    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        showMessage("Completed running ", method, testResult);
    }

    private static void showMessage(String prefix, IInvokedMethod method, ITestResult testResult) {
        String msg = prefix + method.getTestMethod().getMethodName() + "() with the parameters " +
                method.getTestMethod().findMethodParameters(testResult.getTestContext().getCurrentXmlTest());
        System.err.println(msg);
    }
}

Suite xml file

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Sample_Suite" verbose="2">
    <listeners>
        <listener class-name="com.rationaleemotions.stackoverflow.qn48171506.TestListener"/>
    </listeners>
    <test name="sample_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn48171506.SampleTestClass">
                <methods>
                    <include name="helloA">
                        <parameter name="name" value="Jack"/>
                    </include>
                    <include name="helloB">
                        <parameter name="anotherName" value="Daniel"/>
                    </include>
                    <include name="helloC">
                        <parameter name="someOtherName" value="Craig"/>
                    </include>
                </methods>
            </class>
        </classes>
    </test>
</suite>

Execution output

...
... TestNG 6.13.1 by Cédric Beust (cedric@beust.com)
...
About to run helloA() with the parameters {name=Jack}
helloA() says Jack

Completed running helloA() with the parameters {name=Jack}
About to run helloB() with the parameters {anotherName=Daniel}
helloB() says Daniel
Completed running helloB() with the parameters {anotherName=Daniel}

About to run helloC() with the parameters {someOtherName=Craig}
helloC() says Craig
Completed running helloC() with the parameters {someOtherName=Craig}
PASSED: helloA("Jack")
PASSED: helloB("Daniel")
PASSED: helloC("Craig")

===============================================
    sample_test
    Tests run: 3, Failures: 0, Skips: 0
===============================================

===============================================
Test Dependencies
Total tests run: 3, Failures: 0, Skips: 0
===============================================
Krishnan Mahadevan
  • 14,121
  • 6
  • 34
  • 66
  • Thank you for this very well illustrated example. I look forward to trying it soon - much appreciated! – David Jan 10 '18 at 17:59
  • Thanks Krishnan - this works nicely - until I specify the SAME parameter name per method. For example, in your example, if the parameter was named myparam for each method, but the values remained unique. It seems the xml is read, and whatever is the latest stated value for 'myparam' is passed to each method... If this is a limitation of testNG, I'll accept your answer, noting the limitation to be aware of. – David Jan 11 '18 at 11:27
  • @David - Internally TestNG just keeps a map of key/value pairs for parameter/values. So if you are going to be re-using the same key name but with a different value, then the behavior of a map dictates that the key value gets overwritten with the last value. So yeah, you can say that this is kind of like a limitation of TestNG – Krishnan Mahadevan Jan 11 '18 at 11:41
  • @Krishnan Mahadevan, can you please answer to my query below? – Priya P Mar 23 '20 at 14:08