2017-10-29 7 views

答えて

1

これを行うには、基本的にIAnnotationTransformerを活用する必要があります。

これは実際にこれが実行されていることを示すサンプルです。

特定のテストメソッドを複数回実行する必要があることを示すために使用するマーカーアノテーション。

import java.lang.annotation.Retention; 
import java.lang.annotation.Target; 

import static java.lang.annotation.ElementType.METHOD; 

/** 
* A Marker annotation which is used to express the intent that a particular test method 
* can be executed more than one times. The number of times that a test method should be 
* iterated is governed by the JVM argument : <code>-Diteration.count</code>. The default value 
* is <code>3</code> 
*/ 
@Retention(java.lang.annotation.RetentionPolicy.RUNTIME) 
@Target({METHOD}) 
public @interface CanRunMultipleTimes { 
} 

テストクラスは次のようになります。

import org.testng.annotations.Test; 

import java.util.concurrent.atomic.AtomicInteger; 

public class TestClassSample { 
    private volatile AtomicInteger counter = new AtomicInteger(1); 

    @CanRunMultipleTimes 
    @Test 
    public void testMethod() { 
     System.err.println("Running iteration [" + counter.getAndIncrement() + "]"); 
    } 
} 

注釈トランスフォーマの外観は次のとおりです。

import org.testng.IAnnotationTransformer; 
import org.testng.annotations.ITestAnnotation; 

import java.lang.reflect.Constructor; 
import java.lang.reflect.Method; 

public class SimpleAnnotationTransformer implements IAnnotationTransformer { 
    @Override 
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod) { 
     if (testMethod == null || testMethod.getAnnotation(CanRunMultipleTimes.class) == null) { 
      return; 
     } 

     int counter = Integer.parseInt(System.getProperty("iteration.count", "3")); 
     annotation.setInvocationCount(counter); 
    } 
} 

ここでスイートのxmlファイルがどのように見えるかです:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd"> 
<suite name="46998341_Suite" verbose="2"> 
    <listeners> 
     <listener class-name="com.rationaleemotions.stackoverflow.qn46998341.SimpleAnnotationTransformer"/> 
    </listeners> 
    <test name="46998341_Test"> 
     <classes> 
      <class name="com.rationaleemotions.stackoverflow.qn46998341.TestClassSample"/> 
     </classes> 
    </test> 
</suite> 

ここでは、出力は次のようになり方法は次のとおりです。あなたが

... TestNG 6.12 by Cédric Beust ([email protected]) 
... 
Running iteration [1] 
Running iteration [2] 
Running iteration [3] 
PASSED: testMethod 
PASSED: testMethod 
PASSED: testMethod 

=============================================== 
    46998341_Test 
    Tests run: 3, Failures: 0, Skips: 0 
=============================================== 

=============================================== 
46998341_Suite 
Total tests run: 3, Failures: 0, Skips: 0 
=============================================== 
+0

ありがとう! @KrishnanMahadevanサー!あなたは確かに私の師になりました:) –

+0

あなたが助けてくれたら、私の答えを受け入れることができますか? –

関連する問題