首页 文章

如何在标记测试后运行清理方法?

提问于
浏览
2

我正在为我的Java项目编写JUnit 5测试 .

我有一些测试方法,需要耗费时间进行清理(每次测试后) . 理想情况下,我想用一些注释标记它们并仅为它们运行清理方法 .

这是我试过的:

class MyTest {

    @AfterEach
    @Tag("needs-cleanup")
    void cleanup() {
        //do some complex stuff
    }

    @Test
    void test1() {
         //do test1
    }

    @Test
    @Tag("needs-cleanup")
    void test2() {
         //do test2
    }
}

我想 cleanup 方法只能在 test2 之后运行 . 但实际上它在两次测试后都会运行 .

是否可以通过JUnit 5注释的某种组合来实现它?我不想将我的测试类分成几个类或直接从测试方法调用 cleanup .

3 回答

  • 3

    来自文档:

    TestInfo:如果方法参数的类型为TestInfo,则TestInfoParameterResolver将提供与当前测试对应的TestInfo实例作为参数的值 . 然后,可以使用TestInfo检索有关当前测试的信息,例如测试的显示名称,测试类,测试方法或关联的标记 . 显示名称是技术名称,例如测试类或测试方法的名称,或通过@DisplayName配置的自定义名称 . TestInfo充当JUnit 4中TestName规则的替代品 .

    关于上面的描述,您可以使用TestInfo类,它为您提供了应该运行cleanUp的类的信息,然后您需要检查条件并通过检查其标记来允许您想要的那些:

    @AfterEach 
    void afterEach(TestInfo info) {
        if(!info.getTags().contains("cleanItUp")) return; // preconditioning only to needs clean up
            //// Clean up logic Here
    }
    
    
    @Test
    @Tag("cleanItUp")
    void myTest() {
    
    }
    
  • 2

    您可以将test注入测试并检查测试注释的标签:

    class MyTest {
      private TestInfo testInfo;
    
      MyTest(TestInfo testInfo) {
        this.testInfo = testInfo;
      }
    
      @AfterEach
      void cleanup() {
        if (this.testInfo.getTags().contains("needs-cleanup")) {
            // .. do cleanup
        } 
      }
    
      @Test
      void test1() {
         //do test1
      }
    
      @Test
      @Tag("needs-cleanup")
      void test2() {
         //do test2
      }
    
    }
    
  • 2

    您可以创建自己的AfterEachCallback扩展并将其应用于所需的测试方法 . 这个扩展将在每次测试后执行's applied to. Then, using custom annotations, you can link specific cleanup methods with specific tests. Here'是扩展的一个例子:

    import static java.lang.annotation.ElementType.METHOD;
    import static java.lang.annotation.RetentionPolicy.RUNTIME;
    
    import java.lang.annotation.Retention;
    import java.lang.annotation.Target;
    import java.lang.reflect.Method;
    import java.util.List;
    import org.junit.jupiter.api.extension.AfterEachCallback;
    import org.junit.jupiter.api.extension.ExtendWith;
    import org.junit.jupiter.api.extension.ExtensionContext;
    import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
    import org.junit.jupiter.api.extension.ExtensionContext.Store;
    import org.junit.platform.commons.support.AnnotationSupport;
    import org.junit.platform.commons.support.HierarchyTraversalMode;
    
    public class CleanupExtension implements AfterEachCallback {
    
      private static final Namespace NAMESPACE = Namespace.create(CleanupExtension.class);
    
      private static boolean namesMatch(Method method, String name) {
        return method.getAnnotation(CleanMethod.class).value().equals(name);
      }
    
      private static Exception suppressOrReturn(final Exception previouslyThrown,
                                                final Exception newlyThrown) {
        if (previouslyThrown == null) {
          return newlyThrown;
        }
        previouslyThrown.addSuppressed(newlyThrown);
        return previouslyThrown;
      }
    
      @Override
      public void afterEach(final ExtensionContext context) throws Exception {
        final Method testMethod = context.getRequiredTestMethod();
        final Cleanup cleanupAnno = testMethod.getAnnotation(Cleanup.class);
        final String cleanupName = cleanupAnno == null ? "" : cleanupAnno.value();
    
        final List<Method> cleanMethods = getAnnotatedMethods(context);
    
        final Object testInstance = context.getRequiredTestInstance();
        Exception exception = null;
    
        for (final Method method : cleanMethods) {
          if (namesMatch(method, cleanupName)) {
            try {
              method.invoke(testInstance);
            } catch (Exception ex) {
              exception = suppressOrReturn(exception, ex);
            }
          }
        }
    
        if (exception != null) {
          throw exception;
        }
      }
    
      @SuppressWarnings("unchecked")
      private List<Method> getAnnotatedMethods(final ExtensionContext methodContext) {
        // Use parent (Class) context so methods are cached between tests if needed
        final Store store = methodContext.getParent().orElseThrow().getStore(NAMESPACE);
        return store.getOrComputeIfAbsent(
            methodContext.getRequiredTestClass(),
            this::findAnnotatedMethods,
            List.class
        );
      }
    
      private List<Method> findAnnotatedMethods(final Class<?> testClass) {
        final List<Method> cleanMethods = AnnotationSupport.findAnnotatedMethods(testClass,
            CleanMethod.class, HierarchyTraversalMode.TOP_DOWN);
    
    
        for (final Method method : cleanMethods) {
          if (method.getParameterCount() != 0) {
            throw new IllegalStateException("Methods annotated with "
                + CleanMethod.class.getName() + " must not have parameters: "
                + method
            );
          }
        }
    
        return cleanMethods;
      }
    
      @ExtendWith(CleanupExtension.class)
      @Retention(RUNTIME)
      @Target(METHOD)
      public @interface Cleanup {
    
        String value() default "";
    
      }
    
      @Retention(RUNTIME)
      @Target(METHOD)
      public @interface CleanMethod {
    
        String value() default "";
    
      }
    
    }
    

    然后你的测试类看起来像:

    import org.junit.jupiter.api.Test;
    
    class Tests {
    
      @Test
      @CleanupExtension.Cleanup
      void testWithExtension() {
        System.out.println("#testWithExtension()");
      }
    
      @Test
      void testWithoutExtension() {
        System.out.println("#testWithoutExtension()");
      }
    
      @Test
      @CleanupExtension.Cleanup("alternate")
      void testWithExtension_2() {
        System.out.println("#testWithExtension_2()");
      }
    
      @CleanupExtension.CleanMethod
      void performCleanup() {
        System.out.println("#performCleanup()");
      }
    
      @CleanupExtension.CleanMethod("alternate")
      void performCleanup_2() {
        System.out.println("#performCleanup_2()");
      }
    
    }
    

    运行 Tests 我得到以下输出:

    #testWithExtension()
    #performCleanup()
    #testWithExtension_2()
    #performCleanup_2()
    #testWithoutExtension()
    

    此扩展名将应用于使用 CleanupExtension.CleanupExtendWith(CleanupExtension.class) 注释的任何测试方法 . 前一个注释的目的是将配置与也应用扩展的注释相结合 . 然后,在每个测试方法之后,扩展将调用使用 CleanupExtension.CleanMethod 注释的类层次结构中的任何方法 . CleanupCleanMethod 都具有 String 属性 . 此属性是"name",只有 CleanMethodCleanup 测试匹配"name"将被执行 . 这允许您将特定的测试方法链接到特定的清理方法 .


    有关JUnit Jupiter扩展的更多信息,请参阅§5 of the User Guide . 另外,对于 CleanupExtension.Cleanup 我正在使用§3.1.1中描述的元注释/组合注释功能 .

    请注意,这比@Roman Konoval给出的the answer更复杂,但如果你不得不多次这样做,它可能会更加用户友好 . 但是,如果您只需要为一个或两个测试类做这个,我建议使用Roman的答案 .

相关问题