junit不启用事务
JUnit 5 Jupiter API provides various ways to disable or enable a test. There are various annotations to help us easily disable/enable tests based on OS, JRE, System properties etc. We can also execute a script to enable or disable a test. Let’s look at various JUnit annotations to enable and disable a test.
JUnit 5 Jupiter API提供了多种禁用或启用测试的方法。 有各种注释可以帮助我们轻松地基于OS,JRE,系统属性等禁用/启用测试。我们还可以执行脚本来启用或禁用测试。 让我们看一下启用和禁用测试的各种JUnit批注。
JUnit禁用测试方法和类 (JUnit Disable Test Method and Class)
JUnit @Disabled
annotation is the easiest way to disable a test. It can be applied to a test method as well as on the Class itself. If @Disabled
annotation is applied to the class, then all the tests present in the class gets disabled. We can also specify a reason message for documentation purposes.
JUnit @Disabled
批注是禁用测试的最简单方法。 它既可以应用于测试方法,也可以应用于类本身。 如果将@Disabled
注释应用于该类,则该类中存在的所有测试都将被禁用。 我们还可以指定原因消息以用于记录。
@Test
@Disabled
void test() {
assertTrue(3 > 0);
}
@Disabled("Explicitly Disabled")
class DisabledTests {
//all tests disabled
}
基于条件的JUnit禁用/启用测试 (JUnit Disable/Enable Tests Based on Condition)
JUnit Jupiter API provides useful annotations in org.junit.jupiter.api.condition
package to enable or disable tests based on the specified condition. These annotations can be applied to test methods as well as the class itself. Let’s look at them one by one.
JUnit Jupiter API在org.junit.jupiter.api.condition
包中提供了有用的注释,以根据指定条件启用或禁用测试。 这些注释可以应用于测试方法以及类本身。 让我们一一看一下。
@DisabledOnOs和@EnabledOnOs (@DisabledOnOs and @EnabledOnOs)
We can use these annotations to disable or enable a test based on the operating system. Internally, these annotations use System property “os.name” to find out the operating system information.
我们可以使用这些注释来基于操作系统禁用或启用测试。 在内部,这些注释使用系统属性“ os.name”来查找操作系统信息。
@Test
@DisabledOnOs(value= {OS.MAC, OS.WINDOWS})
void test2() {
assertFalse(3 < 0);
}
@Test
@EnabledOnOs(value= {OS.MAC})
void test3() {
assertTrue(System.getProperty("os.name").startsWith("Mac"));
}
@DisabledOnJre和@EnabledOnJre (@DisabledOnJre and @EnabledOnJre)
We can use these annotations to disable/enable test based on the JRE version. Internally, these annotations use System property “java.version” to find out the operating system information.
我们可以使用这些注释基于JRE版本禁用/启用测试。 在内部,这些注释使用系统属性“ java.version”来查找操作系统信息。
@Test
@DisabledOnJre(value= JRE.JAVA_10)
void test4() {
assertFalse(3 < 0);
}
@Test
@EnabledOnJre(value= JRE.JAVA_10)
void test5() {
assertTrue(System.getProperty("java.version").startsWith("10"));
}
@DisabledIfEnvironmentVariable和@EnabledIfEnvironmentVariable (@DisabledIfEnvironmentVariable and @EnabledIfEnvironmentVariable)
We can use these annotations with system environment properties. We need to specify the system property name and the regular expression to match them.
我们可以将这些注释与系统环境属性一起使用。 我们需要指定系统属性名称和正则表达式以使其匹配。
These methods use System.getenv(string name)
to get the system property and match with the specified regex.
这些方法使用System.getenv(string name)
获取系统属性并与指定的正则表达式匹配。
@Test
@DisabledIfEnvironmentVariable(named="USER", matches="pankaj")
void test6() {
assertFalse(3 < 0);
}
@Test
@EnabledIfEnvironmentVariable(named="USER", matches="pankaj")
void test7() {
assertTrue("pankaj".equals(System.getenv("USER")));
}
@DisabledIfSystemProperty和@EnabledIfSystemProperty (@DisabledIfSystemProperty and @EnabledIfSystemProperty)
Similar to environment variables, these methods use system properties and specified regex to disable or enable tests.
与环境变量类似,这些方法使用系统属性和指定的正则表达式来禁用或启用测试。
@Test
//My System Properties "os.name" value is "Mac OS X"
@DisabledIfSystemProperty(named="os.name", matches="Mac.*")
void test8() {
assertFalse(3 < 0);
}
@Test
//My System Properties "user.name" value is "pankaj"
@EnabledIfSystemProperty(named="user.name", matches="pankaj")
void test9() {
assertTrue("pankaj".equals(System.getProperty("user.name")));
}
@DisabledIf和@EnabledIf (@DisabledIf and @EnabledIf)
These annotations are used to execute a script to determine if the tests will be disabled or enabled. The default script engine is Oracle Nashorn; however, the engine attribute may be used to override the default script engine name.
这些批注用于执行脚本,以确定将禁用还是启用测试。 默认脚本引擎是Oracle Nashorn ; 但是,引擎属性可用于覆盖默认脚本引擎名称。
An accessor provides access to a map-like structure via a simple String get(String name)
method. The following property accessors are automatically available within scripts.
访问器通过简单的String get(String name)
方法提供对类似于地图的结构的访问。 以下属性访问器在脚本中自动可用。
- systemEnvironment: Operating system environment variable accessor systemEnvironment:操作系统环境变量访问器
- systemProperty: JVM system property accessor systemProperty:JVM系统属性访问器
Let's look at how we can use these annotations to enable and disable tests.
让我们看看如何使用这些批注来启用和禁用测试。
@Test
@DisabledIf("'pankaj' == systemEnvironment.get('USER')")
void test10() {
assertFalse(3 < 0);
}
@Test
@EnabledIf("'pankaj' == systemProperty.get('user.name')")
void test11() {
assertTrue("pankaj".equals(System.getProperty("user.name")));
}
JUnit禁用测试执行 (JUnit Disable Tests Execution)
When you will execute a test class with disabled methods, it will produce the following view.
当您使用禁用的方法执行测试类时,将产生以下视图。
Note that JUnit skips the test methods that are disabled. If you will Disable the whole test class, then the report will show all the test methods inside it as being skipped.
请注意,JUnit会跳过禁用的测试方法。 如果您将禁用整个测试类,那么报告将显示跳过该类中的所有测试方法。
摘要 (Summary)
JUnit Jupiter API provides extensive support to disable or enable a test based on specified conditions. It's very useful when our test cases are dependent on external factors such as OS, Java Version etc.
JUnit Jupiter API提供了广泛的支持,可以根据指定条件禁用或启用测试。 当我们的测试用例依赖于外部因素(例如OS,Java版本等)时,这将非常有用。
翻译自: https://www.journaldev.com/21697/junit-disable-enable-tests
junit不启用事务