2016-11-14 8 views

답변

2

테스트 스위트를 만들고 여러 테스트를 수행 할 수 있습니까? 몇 가지 클래스 만 테스트 할 수 있습니까?

옵션 (1) (이 선호) :하는 당신이 here

옵션 (2) 볼 수 있습니다 당신은 실제로 @Category를 사용하여이 작업을 수행 할 수 있습니다 설명 된대로 당신은 몇 단계를 수행 할 수 있습니다 아래 :

JUnit 사용자 정의 테스트 @Rule과 간단한 사용자 정의 주석 (아래 명시)을 테스트 케이스에 사용해야합니다. 기본적으로 규칙은 테스트를 실행하기 전에 필요한 조건을 평가합니다. 사전 조건이 충족되면 Test 메서드가 실행되고 그렇지 않으면 Test 메서드가 무시됩니다.

이제 모든 시험 수업을 평소대로 @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 { 
} 
관련 문제