JUnit5用户手册~条件执行

文章介绍了如何使用注解来控制测试方法基于操作系统类型(如MAC,LINUX,WINDOWS)、系统架构(aarch64,x86_64)、Java运行环境版本以及本地是否为GraalVMnativeimage执行。此外,还涉及到了根据系统属性和环境变量来启用或禁用测试的情况,并展示了如何自定义条件来决定测试的执行与否。
摘要由CSDN通过智能技术生成

1、操作系统和系统架构

@Test
@EnabledOnOs(MAC)
void onlyOnMacOs() {
  // ...
}

@TestOnMac
void testOnMac() {
  // ...
}

@Test
@EnabledOnOs({ LINUX, MAC })
void onLinuxOrMac() {
  // ...
}

@Test
@DisabledOnOs(WINDOWS)
void notOnWindows() {
  // ...
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Test
@EnabledOnOs(MAC)
@interface TestOnMac {

}

系统架构

@Test
@EnabledOnOs(architectures = "aarch64")
void onAarch64() {
  // ...
}

@Test
@DisabledOnOs(architectures = "x86_64")
void notOnX86_64() {
  // ...
}

@Test
@EnabledOnOs(value = MAC, architectures = "aarch64")
void onNewMacs() {
  // ...
}

@Test
@DisabledOnOs(value = MAC, architectures = "aarch64")
void notOnNewMacs() {
  // ...
}

2、Java运行环境

@Test
@EnabledOnJre(JAVA_8)
void onlyOnJava8() {
  // ...
}

@Test
@EnabledOnJre({ JAVA_9, JAVA_10 })
void onJava9Or10() {
  // ...
}

@Test
@EnabledForJreRange(min = JAVA_9, max = JAVA_11)
void fromJava9to11() {
  // ...
}

@Test
@EnabledForJreRange(min = JAVA_9)
void fromJava9toCurrentJavaFeatureNumber() {
  // ...
}

@Test
@EnabledForJreRange(max = JAVA_11)
void fromJava8To11() {
  // ...
}

@Test
@DisabledOnJre(JAVA_9)
void notOnJava9() {
  // ...
}

@Test
@DisabledForJreRange(min = JAVA_9, max = JAVA_11)
void notFromJava9to11() {
  // ...
}

@Test
@DisabledForJreRange(min = JAVA_9)
void notFromJava9toCurrentJavaFeatureNumber() {
  // ...
}

@Test
@DisabledForJreRange(max = JAVA_11)
void notFromJava8to11() {
  // ...
}

3、本地镜像条件

是否在GraalVM native image内执行

@Test
@EnabledInNativeImage
void onlyWithinNativeImage() {
  // ...
}

@Test
@DisabledInNativeImage
void neverWithinNativeImage() {
  // ...
}

4、系统属性条件

@Test
@EnabledIfSystemProperty(named = "os.arch", matches = ".*64.*")
void onlyOn64BitArchitectures() {
  // ...
}

@Test
@DisabledIfSystemProperty(named = "ci-server", matches = "true")
void notOnCiServer() {
  // ...
}

5、环境变量条件

@Test
@EnabledIfEnvironmentVariable(named = "ENV", matches = "staging-server")
void onlyOnStagingServer() {
  // ...
}

@Test
@DisabledIfEnvironmentVariable(named = "ENV", matches = ".*development.*")
void notOnDeveloperWorkstation() {
  // ...
}

6、定制条件

@Test
@EnabledIf("customCondition")
void enabled() {
  // ...
}

@Test
@DisabledIf("customCondition")
void disabled() {
  // ...
}

boolean customCondition() {
  return true;
}
package example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;

class ExternalCustomConditionDemo {
  @Test
  @EnabledIf("example.ExternalCondition#customCondition")
  void enabled() {
  // ...
  }
}

class ExternalCondition {
  static boolean customCondition() {
  return true;
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值