2016-11-14 12 views
3

私はJunit Test Suiteを設定しています。クラス内のすべてのテストを実行する標準メソッド(たとえば、here)を使用してテストスイートを設定する方法を知っています。JUnitテストスイート - クラス内のすべてのテストではなく、特定のテストのみを含める?

test suiteを作成して、いくつかの異なるクラスから特定のテストを実行することはできますか?

もしそうなら、どのようにすればよいですか?

答えて

2

テストスイートを作成し、いくつかの異なるクラス から特定のテストを実行することは可能ですか?

オプション(1)(これを好む):あなたが実際にあなたがhere

オプション(2)見ることができため@Category使用してこれを行うことができます。説明したようにあなたはいくつかの手順でこれを行うことができますが

あなたのテストケースでは、JUnitカスタムテスト@Ruleと簡単なカスタムアノテーション(下記)を使用する必要があります。基本的に、ルールはテストを実行する前に必要な条件を評価します。事前条件が満たされている場合は、テストメソッドが実行されます。そうでない場合、テストメソッドは無視されます。

今のところ、いつものようにすべてのテストクラスを@Suiteにする必要があります。

コードを以下に示す:

MyTestConditionカスタム注釈:

@Retention(RetentionPolicy.RUNTIME) 
@Target(ElementType.METHOD) 
public @interface MyTestCondition { 

     public enum Condition { 
       COND1, COND2 
     } 

     Condition condition() default Condition.COND1; 
} 

MyTestRuleクラス:

public class MyTestRule implements TestRule { 

     //Configure CONDITION value from application properties 
    private static String condition = "COND1"; //or set it to COND2 

    @Override 
    public Statement apply(Statement stmt, Description desc) { 

      return new Statement() { 

     @Override 
     public void evaluate() throws Throwable { 

       MyTestCondition ann = desc.getAnnotation(MyTestCondition.class); 

       //Check the CONDITION is met before running the test method 
       if(ann != null && ann.condition().name().equals(condition)) { 
         stmt.evaluate(); 
       } 
     }   
     }; 
    } 
} 

MyTestsクラス:

public class MyTests { 

     @Rule 
     public MyTestRule myProjectTestRule = new MyTestRule(); 

     @Test 
     @MyTestCondition(condition=Condition.COND1) 
     public void testMethod1() { 
       //testMethod1 code here 
     } 

     @Test 
     @MyTestCondition(condition=Condition.COND2) 
     public void testMethod2() { 
       //this test will NOT get executed as COND1 defined in Rule 
       //testMethod2 code here 
     } 

} 

MyTestSuiteクラス:

@RunWith(Suite.class) 
@Suite.SuiteClasses({MyTests.class 
}) 
public class MyTestSuite { 
} 
関連する問題