이 작업은 수행 할 수 있지만 약간의 작업이 필요합니다. 자신의 Suite 러너와 자신의 테스트 러너를 정의한 다음 테스트 러너에서 runChild()를 재정의해야합니다. 다음과 스위트 룸 및 테스트 클래스를 사용 : 나는 @Deprecated
와 test1()
을 주석 한
@RunWith(MySuite.class)
@SuiteClasses({Class1Test.class})
public class AllTests {
}
public class Class1Test {
@Deprecated @Test public void test1() {
System.out.println("" + this.getClass().getName() + " test1");
}
@Test public void test2() {
System.out.println("" + this.getClass().getName() + " test2");
}
}
참고. 당신은 당신이 시험에 @Deprecated
주석이있을 때 뭔가 다른 일을하고 싶은, 그래서 우리는 사용자 정의 Runner
사용하는 스위트 룸을 확장해야합니다
public class MySuite extends Suite {
// copied from Suite
private static Class<?>[] getAnnotatedClasses(Class<?> klass) throws InitializationError {
Suite.SuiteClasses annotation = klass.getAnnotation(Suite.SuiteClasses.class);
if (annotation == null) {
throw new InitializationError(String.format("class '%s' must have a SuiteClasses annotation", klass.getName()));
}
return annotation.value();
}
// copied from Suite
public MySuite(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(null, getRunners(getAnnotatedClasses(klass)));
}
public static List<Runner> getRunners(Class<?>[] classes) throws InitializationError {
List<Runner> runners = new LinkedList<Runner>();
for (Class<?> klazz : classes) {
runners.add(new MyRunner(klazz));
}
return runners;
}
}
의 JUnit 그것을 실행할 각 시험에 대한 Runner
만듭니다. 일반적으로 Suite는 기본값 BlockJUnit4ClassRunner
을 만들 것입니다. 여기서 우리가하는 것은 SuiteClass
주석에서 클래스를 읽는 Suite의 생성자를 재정의하는 것입니다. 그리고 우리는 자신의 주자를 MyRunner
으로 만듭니다. 이것은 MyRunner 클래스입니다.
public class MyRunner extends BlockJUnit4ClassRunner {
public MyRunner(Class<?> klass) throws InitializationError {
super(klass);
}
@Override
protected void runChild(final FrameworkMethod method, RunNotifier notifier) {
Description description= describeChild(method);
if (method.getAnnotation(Ignore.class) != null) {
notifier.fireTestIgnored(description);
} else {
if (description.getAnnotation(Deprecated.class) != null) {
System.out.println("name=" + description.getMethodName() + " annotations=" + description.getAnnotations());
}
runLeaf(methodBlock(method), description, notifier);
}
}
}
대부분이 BlockJUnit4ClassRunner
에서 복사됩니다.내가 추가 한 비트는 다음과 같습니다 우리는 방법에 @Deprecated
주석의 존재 여부를 테스트하고, 뭔가를 할
if (description.getAnnotation(Deprecated.class) != null) {
System.out.println("name=" + description.getMethodName() + " annotations=" + description.getAnnotations());
}
그것은이 있다면. 나머지는 독자를위한 운동으로 남아 있습니다. 위의 Suite를 실행하면 다음과 같이 출력됩니다.
name=test1 annotations=[@java.lang.Deprecated(), @org.junit.Test(expected=class org.junit.Test$None, timeout=0)]
uk.co.farwell.junit.run.Class1Test test1
uk.co.farwell.junit.run.Class1Test test2
Suite에는 호출 방법에 따라 여러 생성자가 있습니다. 위의 작업은 Eclipse에서 작동하지만 Suite를 실행하는 다른 방법은 테스트하지 않았습니다. 자세한 내용은 Suite의 다양한 생성자와 함께 주석을 참조하십시오.
나는 그것이 내가 찾고있는 것이라고 생각하지 않는다. 각 테스트 사이에 무언가가 필요합니다. – ArtB
그러나 테스트 그룹이있는 경우 BeforeMethod 및 AfterMethod를 사용할 수 있어야합니다. (아마도) – ollins
어쨌든, JUnit을 사용해야하고 TestNG는 옵션이 아닙니다. – ArtB