几年前的以下post概述了可以在testng套件xml中指定方法级别参数:
例如 - 需要将ID传递给方法,该方法将是唯一的(每个配置):
<methods>
<include name="testX">
<parameter name="ID" value ="3452"/>
</include>
<include name="testY">
<parameter name="ID" value ="3453"/>
</include>
</methods>
这还有可能吗?我没有在testng文档中看到它。 (我需要在方法级别包含特定参数,而不是测试级别)
如果可以,我怎样才能获得方法参数的值,因为它似乎没有在测试用例类文件中使用@Parameters获取。
引用的帖子确实是指使用:
iTestResult.getMethod().findMethodParameters(iTestContext.getCurrentXmlTest())
但是如果这仍然适用,那么将会有一些关于将它放入正确的监听器方法的指导。
由于
答案 0 :(得分:1)
这非常有可能并且仍然在TestNG中得到支持。 这是一个完整的示例,显示了所有这些内容。
我正在使用TestNG 6.13.1(这是截至今天的最新发布的TestNG版本)
示例测试类
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);
}
}
示例测试用户
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);
}
}
套件xml文件
<?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>
执行输出
...
... 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
===============================================