java - 如何使用 TestNG 和 Java 设置方法执行期间的调用计数

标签 java selenium annotations testng invocation

我有一种测试方法,我手动执行了 200 次。

@Test(priority=2, invocationCount = 200)
public void inviteTalents() throws InterruptedException
{
  logger.log(Status.INFO, "Count " + logins[count]);
}

如何设置调用计数变量?我尝试过类似的方法,但不起作用。有什么帮助吗?

  @BeforeMethod
  public void setUp(Method method, ITestContext context) {

    if(method.getName().equals("test3"))
    {
        ITestNGMethod currentTestNGMethod = null;
        for (ITestNGMethod testNGMethod : context.getAllTestMethods())
        {
          if (testNGMethod.getInstance() == this)
          {
            currentTestNGMethod = testNGMethod;
            break;
          }
        }
        currentTestNGMethod.setInvocationCount(count);  
    }
  }

最佳答案

您可以使用 IAnnotationTransformer 实现来完成此任务。

以下示例展示了如何通过 JVM 参数传入方法名称和调用计数,以及注释转换器实现如何在运行时更改调用计数。

package com.rationaleemotions.stackoverflow.qn51160440;

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;

public class TestClassSample {
  @Test
  public void fooTest() {
    ITestResult r = Reporter.getCurrentTestResult();
    String methodname = r.getMethod().getMethodName();
    System.err.println(
        "Running " + methodname + "() on Thread [" + Thread.currentThread().getId() + "]");
  }
}

这是注释转换器的样子

package com.rationaleemotions.stackoverflow.qn51160440;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import org.testng.IAnnotationTransformer;
import org.testng.annotations.ITestAnnotation;

public class AnnotationTransformerImpl implements IAnnotationTransformer {

  @Override
  public void transform(
      ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) {
    //Pass the value via JVM argument -Dkvp=someMethod=400
    //Here "someMethod" is the name of the method and 400 is the invocation count value
    String kvp = System.getProperty("kvp", "fooTest=200");
    String keyValue[] = kvp.split("=");
    if (keyValue.length != 2) {
      return;
    }
    if (!testMethod.getName().equalsIgnoreCase(keyValue[0])) {
      return;
    }
    annotation.setInvocationCount(Integer.parseInt(keyValue[1]));
    annotation.setThreadPoolSize(25);
  }
}

这是套件文件的样子

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="45160355_Suite" parallel="methods" verbose="2" >
    <listeners>
        <listener
          class-name="com.rationaleemotions.stackoverflow.qn51160440.AnnotationTransformerImpl"/>
    </listeners>
    <test name="45160355_test" verbose="2">
        <classes>
            <class name="com.rationaleemotions.stackoverflow.qn51160440.TestClassSample"/>
        </classes>
    </test>
</suite>

关于java - 如何使用 TestNG 和 Java 设置方法执行期间的调用计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51160440/

相关文章:

google-chrome - Selenium 崩溃: Chrome automation extension has crashed

python - Selenium 返回一个页面源,其中所有标签名称都以 "a0:"为前缀

java - Hibernate注解字段定义

java - 关于Customer Annotation Processor,为什么getElementsAnnotatedWith()返回的set大小为0

android - Lint 构建失败,出现安全错误 "WrongConstant: Incorrect constant"。 IntDef 注释

java - Android 中的 3D 游戏

java - 如何创建一个方法,接受一个数组并查找某个数字的所有出现,并返回该数字出现的所有索引的新数组

python - 我有一个日历选择器。如何使用 Selenium 和 Python 选择可用日期?

java - Spring 4安全规则定义

java - 如何从 uri 显示 PDF)?