如何为testNG套件xml中包含的方法获取(相同)参数?

如何为testNG套件xml中包含的方法获取(相同)参数?,testng,Testng,以下几年前的内容概述了在testng套件xml中指定方法级参数的可能性: 例如-其中需要将ID传递给方法,该方法将是唯一的(每个配置): 但如果这仍然适用,请提供一些指导,将其放在正确的侦听器方法中 谢谢这是非常可能的,并且在TestNG中仍然受支持。 下面是一个完整的示例,它展示了所有这些功能 我正在为此使用TestNG 6.13.1(这是截至今天最新发布的TestNG版本) 样本测试类 package com.rationaliemotions.stackoverflow.qn4817150

以下几年前的内容概述了在testng套件xml中指定方法级参数的可能性:

例如-其中需要将ID传递给方法,该方法将是唯一的(每个配置):

但如果这仍然适用,请提供一些指导,将其放在正确的侦听器方法中


谢谢

这是非常可能的,并且在TestNG中仍然受支持。 下面是一个完整的示例,它展示了所有这些功能

我正在为此使用TestNG 6.13.1(这是截至今天最新发布的TestNG版本)

样本测试类

package com.rationaliemotions.stackoverflow.qn48171506;
导入org.testng.annotations.Parameters;
导入org.testng.annotations.Test;
公共类SampleTestClass{
@试验
@参数(value=“name”)
public void helloA(字符串名称){
System.err.println(“helloA()表示“+name”);
}
@试验
@参数(value=“anotherName”)
public void helloB(字符串名称){
System.err.println(“helloB()表示“+name”);
}
@试验
@参数(value=“someOtherName”)
public void helloC(字符串名称){
System.err.println(“helloC()表示“+name”);
}
}
示例测试侦听器

package com.rationaliemotions.stackoverflow.qn48171506;
导入org.testng.IInvokedMethod;
导入org.testng.IInvokedMethodListener;
导入org.testng.ITestResult;
公共类TestListener实现IInvokedMethodListener{
@凌驾
调用前公共无效(IInvokedMethod方法,ITestResult testResult){
showMessage(“即将运行”、方法、测试结果);
}
@凌驾
调用后公共void(IInvokedMethod方法,ITestResult testResult){
showMessage(“已完成运行”、方法、测试结果);
}
私有静态void showMessage(字符串前缀、IInvokedMethod方法、ITestResult testResult){
字符串msg=prefix+method.getTestMethod().getMethodName()+“(),带参数”+
方法.getTestMethod().findMethodParameters(testResult.getTestContext().getCurrentXmlTest());
系统错误打印项次(msg);
}
}
套件xml文件


执行输出

...
... 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——这很好——直到我为每个方法指定了相同的参数名。例如,在您的示例中,如果每个方法的参数名为myparam,但值保持唯一。似乎xml已被读取,而“myparam”的最新声明值将传递给每个方法。。。如果这是testNG的一个限制,我将接受您的回答,并注意需要注意的限制。@David-内部testNG只保留参数/值的键/值对映射。因此,如果要重新使用相同的键名但使用不同的值,则映射的行为指示键值被最后一个值覆盖。所以,是的,你可以说这有点像一个限制TestNG@KrishnanMahadevan,你能回答我下面的问题吗?
iTestResult.getMethod().findMethodParameters(iTestContext.getCurrentXmlTest())
...
... 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
===============================================