如何获得JUnit测试套件类的注释?



我有这样一个测试套件类的例子,在JUnit 4.13中一次运行多个测试类。

@RunWith(Suite.class)
@SuiteClasses({
FirstTest.class,
SecondTest.class
})
@TestSuiteAnnotation
public class TestSuite {
}

这些是我的测试类。

@FirstAnnotation
public class FirstTest extends ExtTest {
@Test
public void test() {
}
}
@SecondAnnotation
public class SecondTest extends ExtTest {
@Test
public void test() {
}
}

public class ExtTest {
@Before
public void beforeMethod() {
System.out.println("Annotations from " + this.getClass());
Arrays.asList(this.getClass().getAnnotations()).forEach(System.out::println);
}
}
当我从TestSuite.class运行test时,控制台输出是:
Annotations from class FirstTest
@FirstAnnotation()
Annotations from class SecondTest
@SecondAnnotation()

目前,this.getClass().getAnnotations()返回来自测试类的注释(即FirstTest.classSecondTest.class)。当我从TestSuite.class运行测试时,我想获得注释@TestSuiteAnnotation

期望的输出应该是:

Annotations from class FirstTest
@FirstAnnotation()
@TestSuiteAnnotation()
Annotations from class SecondTest
@SecondAnnotation()
@TestSuiteAnnotation()

当我从TestSuite.class运行测试时,我能以某种方式获得注释@TestSuiteAnnotation吗?

你有多个选择:

JUnit 4运行listener

在JUnit 4中,您可以注册RunListener,如@nrainer所示。如果使用Maven构建,很容易像这样注册一个运行侦听器:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
<configuration>
<includes>
<include>org.acme.TestSuite</include>
</includes>
<properties>
<property>
<name>listener</name>
<value>org.acme.SuiteRunListener</value>
</property>
</properties>
</configuration>
</plugin>

运行侦听器可以覆盖事件testSuiteStartedtestSuiteFinished,并直接记录您感兴趣的注释,或者将它们分配给静态线程局部变量,如testSuiteStarted中的private static ThreadLocal<List<Annotation>> currentSuiteAnnotations,然后在testSuiteFinished中再次取消分配。

这在Maven中工作得很好,我测试了它。不幸的是,没有直接支持在IntelliJ IDEA或Eclipse等ide中使用运行侦听器运行测试。因此,如果您想避免像这里所示的那样从一个带有main方法的类中手动运行测试,因为它会带走从套件到测试类再到测试方法的所有良好的IDE测试报告,那么这不是一个选项。

JUnit 5测试执行监听器

与JUnit 4的运行侦听器类似,您可以为JUnit 5的测试注册一个TestExecutionListener。JUnit 5的优势在于,你可以通过Java的ServiceLoader机制全局注册它,也就是说,它将在引导JUnit时被选中,也应该在ide中工作。我用另一种类型的扩展做了类似的事情,它在IntelliJ IDEA中工作得很好,当然在Maven中也很好。

JUnit 4与自定义套件运行器

回到JUnit 4,我们可以通过声明一种特殊类型的套件来扩展第一种方法的运行侦听器。您只需使用该套件而不是org.junit.runners.Suite,就可以在Maven和IDE中享受工作运行侦听器。它是这样工作的,为了方便起见,请参阅我在GitHub上的MCVE:
package org.acme;
import org.junit.runner.Description;
import org.junit.runner.RunWith;
import org.junit.runner.notification.RunListener;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;
import java.lang.annotation.Annotation;
import java.util.List;
import java.util.stream.Collectors;
public class SuiteRunListener extends RunListener {
private static ThreadLocal<String> currentSuiteName = new ThreadLocal<String>();
private static ThreadLocal<List<Annotation>> currentSuiteAnnotations = new ThreadLocal<>();
@Override
public void testSuiteStarted(Description description) throws Exception {
super.testSuiteStarted(description);
final RunWith runWith = description.getAnnotation(RunWith.class);
if (runWith != null && runWith.value().equals(SuiteWithListener.class)) {
currentSuiteName.set(description.getDisplayName());
currentSuiteAnnotations.set(
description.getAnnotations().stream()
.filter(annotation -> {
final Class<? extends Annotation> annotationType = annotation.annotationType();
return !(annotationType.equals(RunWith.class) || annotationType.equals(SuiteClasses.class));
})
.collect(Collectors.toList())
);
}
}
@Override
public void testSuiteFinished(Description description) throws Exception {
super.testSuiteFinished(description);
final RunWith runWith = description.getAnnotation(RunWith.class);
if (runWith != null && runWith.value().equals(SuiteWithListener.class)) {
currentSuiteName.set(null);
currentSuiteAnnotations.set(null);
}
}
public static String getCurrentSuiteName() {
return currentSuiteName.get();
}
public static List<Annotation> getCurrentSuiteAnnotations() {
return currentSuiteAnnotations.get();
}
}
package org.acme;
import org.junit.runner.Runner;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.Suite;
import org.junit.runners.model.InitializationError;
import org.junit.runners.model.RunnerBuilder;
import java.util.List;
public class SuiteWithListener extends Suite {
public SuiteWithListener(Class<?> klass, RunnerBuilder builder) throws InitializationError {
super(klass, builder);
}
public SuiteWithListener(RunnerBuilder builder, Class<?>[] classes) throws InitializationError {
super(builder, classes);
}
protected SuiteWithListener(Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(klass, suiteClasses);
}
protected SuiteWithListener(RunnerBuilder builder, Class<?> klass, Class<?>[] suiteClasses) throws InitializationError {
super(builder, klass, suiteClasses);
}
protected SuiteWithListener(Class<?> klass, List<Runner> runners) throws InitializationError {
super(klass, runners);
}
@Override
public void run(RunNotifier notifier) {
notifier.addListener(new SuiteRunListener());  // !!!
super.run(notifier);
}
}
package org.acme;
import org.junit.runner.RunWith;
import org.junit.runners.Suite.SuiteClasses;
@RunWith(SuiteWithListener.class)  // !!!
@SuiteClasses({
FirstTest.class,
SecondTest.class
})
@TestSuiteAnnotation
public class TestSuite {}
package org.acme;
import org.junit.Before;
import java.util.Arrays;
public class ExtTest {
@Before
public void beforeMethod() {
String currentSuiteName = SuiteRunListener.getCurrentSuiteName();
if (currentSuiteName != null) {
System.out.println("Annotations from suite " + currentSuiteName);
SuiteRunListener.getCurrentSuiteAnnotations().forEach(System.out::println);
}
System.out.println("Annotations from class " + this.getClass());
Arrays.asList(this.getClass().getAnnotations()).forEach(System.out::println);
System.out.println();
}
}

现在,当运行套件时,您应该看到这样的输出:

Annotations from suite org.acme.TestSuite
@org.acme.TestSuiteAnnotation()
Annotations from class class org.acme.FirstTest
@org.acme.FirstAnnotation()
Annotations from suite org.acme.TestSuite
@org.acme.TestSuiteAnnotation()
Annotations from class class org.acme.SecondTest
@org.acme.SecondAnnotation()

请注意:我假设您确实需要从每个测试方法访问当前套件,而不仅仅是在测试类或套件级别。如果您不需要这些,并且在套件启动和/或完成时让运行侦听器做一些事情就足够了,那么当然不需要当前套件名称和套件注释的getter方法。我只是扩展了你自己的例子。

相关内容

  • 没有找到相关文章

最新更新