【问题标题】:How to set invocation count of a test method using TestNG listeners?如何使用 TestNG 侦听器设置测试方法的调用计数?
【发布时间】:2016-08-23 12:52:10
【问题描述】:

我正在尝试为特定测试用例设置调用计数。以下代码是为侦听器编写的。目标是将特定测试方法循环给定次数。侦听器工作正常,但 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() {

}

【问题讨论】:

    标签: java automation automated-tests testng listeners


    【解决方案1】:

    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);
        }
      }
    }
    

    【讨论】:

    • 您能否更深入地了解为什么这是一个更好的听众?
    • 因为 IAnnotationTransformer 在运行测试(初始化阶段)之前由 TestNG 使用,而在运行期间使用 IInvokedMethodListener。在运行阶段更改值可能有效,也可能无效。
    • 谢谢你,朱利安先生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    相关资源
    最近更新 更多