好的,所以我有这样一个类
public class Calculator {
@Test(dataProvider = "dp")
public void add(int a, int b) {
System.out.println("Invoked add: a, b" + a + "," + b);
}
@DataProvider(name = "dp")
public Object[][] createData(ITestContext ctx) {
return new Object[][] { new Object[] { 1, 2 }, new Object[] { 2, 3 } };
}
当测试运行时,它会运行两次 add 方法。我想根据其输入唯一地跟踪 add 的每次调用。所以说,add 是用 1,2 作为输入调用的,那么这是一个独特的调用。如果失败,我想将此信息存储到具有调用 ID 的数据库中。
如何使用 testng 实现此目的?所有的监听器(methodinvocationlistener 等)似乎都没有提供唯一标识方法运行的上下文。是的,它们确实让您看到参数,但我无法跟踪单个参数。那么,我是否以某种方式将我自己的唯一参数注入(inject)结果对象并从那里跟踪它?
更新
我正在添加改进的代码,以帮助更好地理解上下文。 这是我的testng.xml
<suite name="Default Suite">
<test name="test">
<classes>
<class name="com.test.testng.Calculator">
<methods>
<include name="add">
<parameter name="data-id" value="1"/>
</include> <!-- add -->
<include name="add">
<parameter name="data-id" value="2"/>
</include> <!-- add -->
<include name="subtract">
<parameter name="data-id" value="3"/>
</include> <!-- subtract -->
</methods>
</class> <!-- com.test.testng.Calculator -->
</classes>
</test> <!-- test -->
</suite> <!-- Default Suite -->
我有两次加法调用和一次减法调用。 这是我的数据提供者
public class Calculator {
@Test(dataProvider = "dp")
public void add(int first, int second) {
System.out.println("invoked add");
}
@Test(dataProvider = "dp")
public void subtract(int first, int second) {
System.out.println("invoked subtract");
}
@DataProvider(name = "dp")
public Object[][] createData(Method m, ITestContext ctx) {
Object[][] data = new Object[][] { new Object[] { 1, 2 }, new Object[] { 2, 3 }, new Object[] { 3, 4 } };
for (XmlClass test : ctx.getCurrentXmlTest().getXmlClasses()) {
for (XmlInclude method : test.getIncludedMethods()) {
if (method.getName().equals(m.getName()))
int key = Integer.parseInt(method.getAllParameters().get("data-id"));
return new Object[][] { data[key - 1] };
}
}
return null ;
}
}
我预计,add 会运行两次,一次以 1,2 作为输入,另一次以 2,3 作为输入。同样,减去 3,4 作为输入。但是,我看到的是这个——
[SuiteRunner] Created 1 TestRunners
[TestRunner] Running test test on 1 classes, included groups:[] excluded groups:[]
===== Test class
com.test.testng.Calculator
@Test Calculator.add(int, int)[pri:0, instance:com.test.testng.Calculator@39a054a5]
@Test Calculator.subtract(int, int)[pri:0, instance:com.test.testng.Calculator@39a054a5]
======
method.getAllParamas(){data-id=1}
[Invoker 665576141] Invoking com.test.testng.Calculator.add
invoked
[Invoker 665576141] Invoking com.test.testng.Calculator.subtract
subtract
===== Invoked methods
Calculator.add(int, int)[pri:0, instance:com.test.testng.Calculator@39a054a5]1 2 966808741
Calculator.subtract(int, int)[pri:0, instance:com.test.testng.Calculator@39a054a5]1 2 966808741
=====
我需要根据将从 testng xml 发送的特殊参数向每个方法提供数据。我如何实现这一点?
最佳答案
为此,您可以在 testng.xml 中定义不同的测试用例,如下所示:
<suite name="Default Suite">
<test name="test">
<classes>
<class name="com.test.testng.Calculator">
<methods>
<include name="add">
<parameter name="data-id" value="1"/>
</include> <!-- add -->
<include name="subtract">
<parameter name="data-id" value="3"/>
</include> <!-- subtract -->
</methods>
</class> <!-- com.test.testng.Calculator -->
</classes>
</test> <!-- test -->
<test name="test2">
<classes>
<class name="com.test.testng.Calculator">
<methods>
<include name="add">
<parameter name="data-id" value="2"/>
</include> <!-- add -->
</methods>
</class> <!-- com.test.testng.Calculator -->
</classes>
</test> <!-- test -->
</suite> <!-- Default Suite -->
我添加一个登录提供者方法:
package com.test.testng;
import java.lang.reflect.Method;
import org.testng.ITestContext;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
public class Calculator {
@Test(dataProvider = "dp")
public void add(int first, int second) {
System.out.println("invoked add");
}
@Test(dataProvider = "dp")
public void subtract(int first, int second) {
System.out.println("invoked subtract");
}
@DataProvider(name = "dp")
public Object[][] createData(Method m, ITestContext ctx) {
Object[][] data = new Object[][] { new Object[] { 1, 2 }, new Object[] { 2, 3 }, new Object[] { 3, 4 } };
for (XmlClass test : ctx.getCurrentXmlTest().getXmlClasses()) {
for (XmlInclude method : test.getIncludedMethods()) {
if (method.getName().equals(m.getName())) {
int key = Integer.parseInt(method.getAllParameters().get("data-id"));
System.out.println("Running method " + m.getName() + " with data-id: " + key);
return new Object[][] { data[key - 1] };
}
}
}
return null ;
}
}
将此 xml 作为 testng 套件(使用 eclipse 插件)运行显示:
[TestNG] Running:
/Users/fhernandez/Documents/workspaceTest/testNg-test/src/test/resources/testng.xml
Running method add with data-id: 1
invoked add
Running method subtract with data-id: 3
invoked subtract
Running method add with data-id: 2
invoked add
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
如果我很好地理解了你的要求,你就可以实现它。
实现此目的的另一种方法是在测试类之前放置一个方面或代理,并检查方法调用,在这个方面/代理中实现您的要求。
更新
我给计算器添加了一个监听器
@Listeners(Listener.class)
public class Calculator
监听器如下所示
package com.test.testng;
import java.util.Arrays;
import org.testng.IInvokedMethod;
import org.testng.IInvokedMethodListener;
import org.testng.ITestResult;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlInclude;
public class Listener implements IInvokedMethodListener {
public void afterInvocation(IInvokedMethod method, ITestResult itr) {
// TODO implements
}
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {
// Parameters value
System.out.println("Parameters invocation value for method " + method.getTestMethod().getMethodName());
Arrays.asList(testResult.getParameters()).stream().forEach(System.out::println);
// get data-id
for (XmlClass test : testResult.getTestContext().getCurrentXmlTest().getXmlClasses()) {
for (XmlInclude met : test.getIncludedMethods()) {
if (met.getName().equals(method.getTestMethod().getMethodName())) {
int key = Integer.parseInt(met.getAllParameters().get("data-id"));
System.out.println("listener: Running method " + method.getTestMethod().getMethodName() + " with data-id: " + key);
}
}
}
}
}
beforeInvocation 中的代码显示参数调用值和 data-id,这是输出
[TestNG] Running:
/Users/fhernandez/Documents/workspaceTest/testNg-test/src/test/resources/testng.xml
Running method add with data-id: 1
Parameters invocation value for method add
1
2
listener: Running method add with data-id: 1
invoked add
Running method subtract with data-id: 3
Parameters invocation value for method subtract
3
4
listener: Running method subtract with data-id: 3
invoked subtract
Running method add with data-id: 2
Parameters invocation value for method add
2
3
listener: Running method add with data-id: 2
invoked add
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
关于java - 唯一标识 testng 中的每个测试调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33364109/