gpt4 book ai didi

java - 如何使用 TestNG 监听器设置测试方法的调用计数?

转载 作者:行者123 更新时间:2023-11-29 04:41:10 24 4
gpt4 key购买 nike

我正在尝试为特定测试用例设置调用计数。以下代码是为监听器编写的。目标是循环特定的测试方法给定的次数。监听器工作正常,但 setInvocationCount 未按预期工作。

监听类:-

public class InvokedListener implements IInvokedMethodListener {

String count = System.getProperty("count", "100");
int counter = Integer.parseInt(count);
int count1;

@Override
public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {

System.out.println("before invocation of " + method.getTestMethod().getMethodName());
String methodName = method.getTestMethod().getMethodName();

if (methodName.contains("TC_02_InstructorCreatesCourse")) {
System.out.println("The listener is activated for:-" + method.getTestMethod().getMethodName());
method.getTestMethod().setInvocationCount(20);
System.out.println("Invocation count is set to :-" + counter);
}
}

@Override
public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
System.out.println("after invocation " + method.getTestMethod().getMethodName());
}

testNG xml:-

<?xml version="1.0" encoding="UTF-8"?>

        <listener class-name="InvokedLister" />

</listeners>

<test name="CourseCreation"
preserve-order="true" enabled="true">
<classes>
<class
name="TestCases" />
</classes>
</test>

测试用例:-

@Test
public void TC_01_LoginToSSOApplicationViaInstructor() {
System.out.println("1");
}

@Test
public void TC_02_InstructorCreatesCourse() {
System.out.println("2");

}

@Test
public void TC_03_LoginToSSApplicationViaStudent() {
System.out.println("3");
}

@Test
public void TC_04_EnrollStudentInCourse() {

}

最佳答案

IAnnotationTransformer or IAnnotationTransformer2是更适合您的听众选择:

public class MyTransformer implements IAnnotationTransformer {

private final int counter;

public MyTransformer() {
String count = System.getProperty("count", "100");
counter = Integer.parseInt(count);
}

public void transform(ITest annotation, Class<?> testClass,
Constructor testConstructor, Method testMethod) {
if (testMethod.getName().contains("TC_02_InstructorCreatesCourse")) {
System.out.println("The listener is activated for:-" + testMethod.getName());
annotation.setInvocationCount(20);
System.out.println("Invocation count is set to :-" + counter);
}
}
}

关于java - 如何使用 TestNG 监听器设置测试方法的调用计数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39101765/

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