gpt4 book ai didi

testng - 如何获取 Testng - 如果测试用例具有具有多个值的数据提供程序,则测试方法名称

转载 作者:行者123 更新时间:2023-12-04 01:56:06 25 4
gpt4 key购买 nike

@DataProvider(name = "Payment")
public static Object[][] Payment() {
return new Object[][] {
{ "TC_001", "Payment 1" },
{ "TC_002", "Payment 2" }
};
}

@Test(enabled = true, dataProvider = "Payment")
public void Payment(String testCaseNumber, String testDesc) throws Exception {
}

@AfterMethod
public void tearDown(ITestResult result) {
testMethodName = result.getMethod().getMethodName();
}

这里的testMethodName只返回Payment

testMethodName 还应返回带有数据提供者名称的付款。

请帮忙

最佳答案

下面的示例展示了如何提取这些值。

import java.lang.reflect.Method;
import java.util.Arrays;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class TestClassSample {

@Test(dataProvider = "dp")
public void testMethod(int i) {}

@DataProvider(name = "dp")
public Object[][] getData() {
return new Object[][] {{1}, {2}};
}

@AfterMethod
public void afterMethod(ITestResult result) {
Object[] parameters = result.getParameters();
Method method = result.getMethod().getConstructorOrMethod().getMethod();
String msg = "";
Test test = method.getAnnotation(Test.class);
if (test == null) {
return;
}
msg += "The @Test method [" + method.getName() + "] ";
String dataProviderName = test.dataProvider();
Class clazz = test.dataProviderClass();
if (!dataProviderName.trim().isEmpty()) {
msg += " had its data provider name as [" + dataProviderName + "] ";
if (clazz != Object.class) {
msg += " and class as [" + clazz.getCanonicalName() + "] ";
}
}
if (parameters != null) {
msg += " and had parameters as " + Arrays.toString(parameters);
}
System.err.println(msg);
}
}

这是输出

The @Test method [testMethod]  had its data provider name as [dp]  and had parameters as [1]
The @Test method [testMethod] had its data provider name as [dp] and had parameters as [2]

===============================================
Default Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================

关于testng - 如何获取 Testng - 如果测试用例具有具有多个值的数据提供程序,则测试方法名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50591109/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com