JUnit 5 中是否有任何 API 来测试包含或排除的标签?
Is there any API in JUnit 5 to test for included or excluded Tags?
如果某个标签是 "included",我想启用测试,即通过 ConsoleLauncher 的选项 --include-tag
或 useJUnitPlatform.includeTags
属性 22=]。在测试class或方法的上下文中是否有任何API来检索此选项的值?
我尝试了基于脚本的条件 @EnabledIf,如下所示:
@EnabledIf("'true' == systemProperty.get('itest.backendSystemPresent') || junitTags.contains('BackendSystemIT') == true")
但是 junitTags
包含相关元素的 @Tag 注释,而不是运行时包含的标签。
Is there any API to retrieve the value (of this option) of all tags that are attached directly or inherited in the context of test class or method?
是的。在您的测试方法中声明并使用 org.junit.jupiter.api.TestInfo
参数。
@Test
@DisplayName("TEST 1")
@Tag("my-tag")
void test1(TestInfo testInfo) {
assertEquals("TEST 1", testInfo.getDisplayName());
assertTrue(testInfo.getTags().contains("my-tag"));
}
详情见https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection
But junitTags contains the @Tag annotations of the element in question, not the tags included at runtime.
这是预期的行为 -- 平台(此处:控制台启动器)已经应用了通过 --include-tag
和其他配置参数传递的过滤器。简而言之:无需在标准 Jupiter 测试中手动检查标签。如果内置过滤有问题,请在此处创建问题:https://github.com/junit-team/junit5/issues/new/choose
再次阅读你的问题,我的回答是"No"。您不能使用 junitTags
来实现您的目标。不,目前没有这样的 API。你需要这样的东西:
@EnabledIf("'true' == evaluateTagExpression('BackendSystemIT') || ...)
因为你还需要注意这里的标签表达式:https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions
但是,标签会在流程的早期进行评估。当标签评估已排除测试时,您的条件将没有机会执行。所以,我想,你必须坚持使用单一系统 属性 开关来控制测试方法的启用状态。
顺便说一句。我们很快就会用 any()
和 none()
标记改进标签表达式语言。 https://github.com/junit-team/junit5/issues/1679
可能的解决方案:
- 用
@Tag("BackendSystemIT")
注释你的测试
- 在 运行测试之前,检查
itest.backendSystemPresent
系统 属性,如果已设置,则将 --include-tag "BackendSystemIT"
传递给测试 运行。
- 让 Jupiter 完成评估标签表达式的工作
如果某个标签是 "included",我想启用测试,即通过 ConsoleLauncher 的选项 --include-tag
或 useJUnitPlatform.includeTags
属性 22=]。在测试class或方法的上下文中是否有任何API来检索此选项的值?
我尝试了基于脚本的条件 @EnabledIf,如下所示:
@EnabledIf("'true' == systemProperty.get('itest.backendSystemPresent') || junitTags.contains('BackendSystemIT') == true")
但是 junitTags
包含相关元素的 @Tag 注释,而不是运行时包含的标签。
Is there any API to retrieve the value (of this option) of all tags that are attached directly or inherited in the context of test class or method?
是的。在您的测试方法中声明并使用 org.junit.jupiter.api.TestInfo
参数。
@Test
@DisplayName("TEST 1")
@Tag("my-tag")
void test1(TestInfo testInfo) {
assertEquals("TEST 1", testInfo.getDisplayName());
assertTrue(testInfo.getTags().contains("my-tag"));
}
详情见https://junit.org/junit5/docs/current/user-guide/#writing-tests-dependency-injection
But junitTags contains the @Tag annotations of the element in question, not the tags included at runtime.
这是预期的行为 -- 平台(此处:控制台启动器)已经应用了通过 --include-tag
和其他配置参数传递的过滤器。简而言之:无需在标准 Jupiter 测试中手动检查标签。如果内置过滤有问题,请在此处创建问题:https://github.com/junit-team/junit5/issues/new/choose
再次阅读你的问题,我的回答是"No"。您不能使用 junitTags
来实现您的目标。不,目前没有这样的 API。你需要这样的东西:
@EnabledIf("'true' == evaluateTagExpression('BackendSystemIT') || ...)
因为你还需要注意这里的标签表达式:https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions
但是,标签会在流程的早期进行评估。当标签评估已排除测试时,您的条件将没有机会执行。所以,我想,你必须坚持使用单一系统 属性 开关来控制测试方法的启用状态。
顺便说一句。我们很快就会用 any()
和 none()
标记改进标签表达式语言。 https://github.com/junit-team/junit5/issues/1679
可能的解决方案:
- 用
@Tag("BackendSystemIT")
注释你的测试
- 在 运行测试之前,检查
itest.backendSystemPresent
系统 属性,如果已设置,则将--include-tag "BackendSystemIT"
传递给测试 运行。 - 让 Jupiter 完成评估标签表达式的工作