26

現在、条件に基づいて TestNG テストを無効にする方法はありますか

現在、TestNG のようにテストを無効にできることはわかっています。

@Test(enabled=false, group={"blah"})
public void testCurrency(){
...
}

条件に基づいて同じテストを無効にしたいのですが、方法がわかりません。このようなもの:

@Test(enabled={isUk() ? false : true), group={"blah"})
public void testCurrency(){
...
}

これが可能かどうかは、誰にでもわかります。

4

7 に答える 7

39

より簡単なオプションは、条件をチェックするメソッドで@BeforeMethodアノテーションを使用することです。テストをスキップしたい場合は、SkipExceptionをスローするだけです。このような:

@BeforeMethod
protected void checkEnvironment() {
  if (!resourceAvailable) {
    throw new SkipException("Skipping tests because resource was not available.");
  }
}
于 2010-12-13T22:36:38.280 に答える
16

次の 2 つのオプションがあります。

アノテーション トランスフォーマーは条件をテストし、条件が満たされない場合は @Test アノテーションをオーバーライドして属性 "enabled=false" を追加します。

于 2010-10-15T20:57:25.670 に答える
10

TestNG で「無効化」テストを制御できるようにする方法が 2 つあります。

注意すべき非常に重要な違いは、指定した条件に基づいて IAnnotationTransformer を実装する際にリフレクションを使用して個々のテストを無効にする間、SkipException が後続のすべてのテストを中断することです。SkipException と IAnnotationTransfomer の両方について説明します。

スキップ例外の例

import org.testng.*;
import org.testng.annotations.*;

public class TestSuite
{
    // You set this however you like.
    boolean myCondition;
    
    // Execute before each test is run
    @BeforeMethod
    public void before(Method methodName){
        // check condition, note once you condition is met the rest of the tests will be skipped as well
        if(myCondition)
            throw new SkipException();
    }
    
    @Test(priority = 1)
    public void test1(){}
    
    @Test(priority = 2)
    public void test2(){}
    
    @Test(priority = 3)
    public void test3(){}
}

IAnnotationTransformer の例

もう少し複雑ですが、その背後にある考え方は反射と呼ばれる概念です。

ウィキ - http://en.wikipedia.org/wiki/Reflection_(computer_programming)

最初に IAnnotation インターフェイスを実装し、これを *.java ファイルに保存します。

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

public class Transformer implements IAnnotationTransformer {

    // Do not worry about calling this method as testNG calls it behind the scenes before EVERY method (or test).
    // It will disable single tests, not the entire suite like SkipException
    public void transform(ITestAnnotation annotation, Class testClass, Constructor testConstructor, Method testMethod){

        // If we have chose not to run this test then disable it.
        if (disableMe()){
            annotation.setEnabled(false);
        }
    }

    // logic YOU control
    private boolean disableMe() {
    }
}

次に、テストスイートのJavaファイルで、 @BeforeClass 関数で次のことを行います

import org.testng.*;
import org.testng.annotations.*;

/* Execute before the tests run. */    
@BeforeClass
public void before(){

    TestNG testNG = new TestNG();
    testNG.setAnnotationTransformer(new Transformer());
}

@Test(priority = 1)
public void test1(){}

@Test(priority = 2)
public void test2(){}

@Test(priority = 3)
public void test3(){}

最後のステップは、build.xml ファイルにリスナーを確実に追加することです。私は最終的に次のようになりました。これはbuild.xmlからの1行です。

<testng classpath="${test.classpath}:${build.dir}" outputdir="${report.dir}" 
    haltonfailure="false" useDefaultListeners="true"
    listeners="org.uncommons.reportng.HTMLReporter,org.uncommons.reportng.JUnitXMLReporter,Transformer" 
    classpathref="reportnglibs"></testng>
于 2014-03-18T20:46:28.943 に答える
3

環境設定に基づいていくつかのテストを無効/スキップするためのこの注釈ベースの方法を好みます。保守が容易で、特別なコーディング技術は必要ありません。

  • IInvokedMethodListener インターフェイスの使用
  • @SkipInHeadlessMode などのカスタム アノテーションを作成します。
  • SkipException をスローする
public class ConditionalSkipTestAnalyzer implements IInvokedMethodListener {
    protected static PropertiesHandler properties = new PropertiesHandler();

    @Override
    public void beforeInvocation(IInvokedMethod invokedMethod, ITestResult result) {
        Method method = result.getMethod().getConstructorOrMethod().getMethod();
        if (method == null) {
            return;
        }
        if (method.isAnnotationPresent(SkipInHeadlessMode.class)
                && properties.isHeadlessMode()) {
            throw new SkipException("These Tests shouldn't be run in HEADLESS mode!");
        }
    }

    @Override
    public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) {
        //Auto generated
    }
}

詳細を確認してください: https://www.lenar.io/skip-testng-tests-based-condition-using-iinvokedmethodlistener/

于 2020-02-10T08:57:28.597 に答える