简体   繁体   English

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

[英]How to set invocation count during execution for the method using TestNG & Java

I am having one test method, which I am manually executing for 200 times. 我有一种测试方法,我手动执行了200次。

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

How can I set the invocation count variable? 如何设置调用计数变量? I have tried something like this, but doesn't work. 我已经尝试过类似的方法,但是没有用。 Any help? 有什么帮助吗?

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

You can make use an IAnnotationTransformer implementation to get this done. 您可以使用IAnnotationTransformer实现来完成此操作。

Here's a sample that shows how to pass in the method name and the invocation count via JVM arguments and how the annotation transformer implementation changes the invocation count in runtime. 这是一个示例,该示例显示如何通过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() + "]");
  }
}

Here's how the annotation transformer looks like 这是注释转换器的外观

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

Here's how the suite file looks like 这是套件文件的外观

<?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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用TestNG侦听器设置测试方法的调用计数? - How to set invocation count of a test method using TestNG listeners? 在执行方法期间如何设置测试方法的调用计数 - How to set invocation count of a test method during execute the method Java - 方法调用和执行 - Java - Method Invocation & Execution 如何使用TestNG + JAVA Reflection设置执行测试方法的优先级 - How to set priority for test methods execution using TestNG + JAVA Reflection 如何设置cucumber testng框架并行执行的线程数? - How to set the thread count in parallel execution of cucumber testng framework? 继承期间TestNG中@BeforeClass方法的执行 - @BeforeClass method execution in TestNG during inheritence 如何在TestNG中获取有关方法调用的测试用例数据? - How to get testcase data on method invocation in TestNG? 错误:方法调用期间引发了 Java 异常 - Error: Java exception was raised during method invocation 使用Maven使用TestNG执行Selenium测试期间出现“ java.lang.Error:未解决的编译问题” - “java.lang.Error: Unresolved compilation problems” during execution Selenium tests with TestNG using Maven 在使用TestNg ITestResult接口或任何其他testNg接口进行测试执行之后,如何调用TestNg @Test方法的输入参数的实例 - How to call instances of input arguments of TestNg @Test method, after test execution using TestNg ITestResult interface or any other testNg interface
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM