如何在 TestNG 中获取有关方法调用的测试用例数据?
How to get testcase data on method invocation in TestNG?
我正在使用 TestNG 包中的 IInvokedMethodListener 来监听我的测试用例的每个方法执行。在每种方法中,我都试图检索每个案例的数据(测试用例数据)。
我一直在搜索 API,但找不到任何有用的方法。有没有人尝试过类似的东西并且成功了?
这是您的操作方法。
一个测试 class 样本,该样本从套件 xml 中获取参数。
import org.assertj.core.api.Assertions;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestClassUsingParameters {
@Parameters({"studentName", "studentAge"})
@Test
public void testMethod(String name, int age) {
Assertions.assertThat(name).isNotEmpty();
Assertions.assertThat(age).isGreaterThan(0);
}
}
一个测试 class 样本,它从数据提供者那里获取参数。
import org.assertj.core.api.Assertions;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestClassUsingDataProvider {
@Test(dataProvider = "dp")
public void testMethod(String name, int age) {
Assertions.assertThat(name).isNotEmpty();
Assertions.assertThat(age).isGreaterThan(0);
}
@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][]{
{"Jack Daniels", 10},
{"Napolean", 15}
};
}
}
这是监听器实现的样子,它会打印在任一情况下馈送到测试方法中的参数。
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import java.util.Arrays;
public class SampleListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Object[] parameters = testResult.getParameters();
if (parameters != null) {
printer("beforeInvocation", method, parameters);
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
Object[] parameters = testResult.getParameters();
if (parameters != null) {
printer("afterInvocation", method, parameters);
}
}
private void printer(String prefix, IInvokedMethod method, Object[] parameters) {
String msg = String.format("Running %s() for method %s() with parameters: %s", prefix,
method.getTestMethod().getMethodName(),
Arrays.toString(parameters));
System.err.println(msg);
}
}
最后是套件 xml 的样子
<?xml version="1.0"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48697918_suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.Whosebug.qn48697918.SampleListener"/>
</listeners>
<test name="48697918_test1">
<classes>
<class name="com.rationaleemotions.Whosebug.qn48697918.TestClassUsingParameters">
<parameter name="studentName" value="Jack Daniels"/>
<parameter name="studentAge" value="15"/>
</class>
<class name="com.rationaleemotions.Whosebug.qn48697918.TestClassUsingDataProvider"/>
</classes>
</test>
</suite>
这是输出:
...
... TestNG 6.14.2 by Cédric Beust (cedric@beust.com)
...
Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 15]
Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 15]
Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running beforeInvocation() for method testMethod() with parameters: [Napolean, 15]
Running afterInvocation() for method testMethod() with parameters: [Napolean, 15]
PASSED: testMethod("Jack Daniels", 15)
PASSED: testMethod("Jack Daniels", 10)
PASSED: testMethod("Napolean", 15)
===============================================
48697918_test1
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
48697918_suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
我正在使用 TestNG 包中的 IInvokedMethodListener 来监听我的测试用例的每个方法执行。在每种方法中,我都试图检索每个案例的数据(测试用例数据)。
我一直在搜索 API,但找不到任何有用的方法。有没有人尝试过类似的东西并且成功了?
这是您的操作方法。
一个测试 class 样本,该样本从套件 xml 中获取参数。
import org.assertj.core.api.Assertions;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class TestClassUsingParameters {
@Parameters({"studentName", "studentAge"})
@Test
public void testMethod(String name, int age) {
Assertions.assertThat(name).isNotEmpty();
Assertions.assertThat(age).isGreaterThan(0);
}
}
一个测试 class 样本,它从数据提供者那里获取参数。
import org.assertj.core.api.Assertions;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class TestClassUsingDataProvider {
@Test(dataProvider = "dp")
public void testMethod(String name, int age) {
Assertions.assertThat(name).isNotEmpty();
Assertions.assertThat(age).isGreaterThan(0);
}
@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][]{
{"Jack Daniels", 10},
{"Napolean", 15}
};
}
}
这是监听器实现的样子,它会打印在任一情况下馈送到测试方法中的参数。
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import java.util.Arrays;
public class SampleListener implements IInvokedMethodListener {
@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
Object[] parameters = testResult.getParameters();
if (parameters != null) {
printer("beforeInvocation", method, parameters);
}
}
@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
Object[] parameters = testResult.getParameters();
if (parameters != null) {
printer("afterInvocation", method, parameters);
}
}
private void printer(String prefix, IInvokedMethod method, Object[] parameters) {
String msg = String.format("Running %s() for method %s() with parameters: %s", prefix,
method.getTestMethod().getMethodName(),
Arrays.toString(parameters));
System.err.println(msg);
}
}
最后是套件 xml 的样子
<?xml version="1.0"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="48697918_suite" verbose="2">
<listeners>
<listener class-name="com.rationaleemotions.Whosebug.qn48697918.SampleListener"/>
</listeners>
<test name="48697918_test1">
<classes>
<class name="com.rationaleemotions.Whosebug.qn48697918.TestClassUsingParameters">
<parameter name="studentName" value="Jack Daniels"/>
<parameter name="studentAge" value="15"/>
</class>
<class name="com.rationaleemotions.Whosebug.qn48697918.TestClassUsingDataProvider"/>
</classes>
</test>
</suite>
这是输出:
...
... TestNG 6.14.2 by Cédric Beust (cedric@beust.com)
...
Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 15]
Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 15]
Running beforeInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running afterInvocation() for method testMethod() with parameters: [Jack Daniels, 10]
Running beforeInvocation() for method testMethod() with parameters: [Napolean, 15]
Running afterInvocation() for method testMethod() with parameters: [Napolean, 15]
PASSED: testMethod("Jack Daniels", 15)
PASSED: testMethod("Jack Daniels", 10)
PASSED: testMethod("Napolean", 15)
===============================================
48697918_test1
Tests run: 3, Failures: 0, Skips: 0
===============================================
===============================================
48697918_suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================