JUnit 5 @EnabledIfSystemProperty 没有按预期工作
JUnit 5 @EnabledIfSystemProperty doesn't work as expected
我将我的测试从 JUnit 4 迁移到 JUnit 5。一切正常,但我之前注释的翻译:
@IfProfileValue(name = "run.import.tests", values = {"true"})
进入
@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
没有按预期工作。在迁移之前,我运行了我的测试并传递了参数
-Drun.import.tests=true
只有当我通过它时,它们才会运行。使用 Junit 5,即使使用注释 @EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
,即使未设置参数 run.import.tests
,测试也会运行。
我是不是做错了什么?
为了让它工作,必须添加一个“相反”的注释,所以它们看起来像这样:
@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
@DisabledIfSystemProperty(named = "run.import.tests", matches = "(?!true)")
我已经检查过了,如果未设置 run.import.tests
属性 或设置为 true
以外的任何其他值,测试 class 将被禁用;如果该值设置为 true
- 测试 class 未禁用。
奇怪的是,the documentation of @EnabledIfSystemProperty 指出:
If the specified system property is undefined, the annotated class or method will be disabled.
然而它不能那样工作,它可能是一个错误。我将尝试调试 JUnit classes,如果我在它们的 GitHub 上创建问题,我将在此处 link 它。
我已经检查了代码并对其进行了多次测试 - 这是摘要:
- 当我 运行 使用 Maven (
mvn test
) 进行测试时,注释 @EnabledIfSystemProperty
单独工作正常 - 只有当我添加 [=16] 时测试才 运行 =] 参数。在这种情况下不需要 @DisabledIfSystemProperty
。
- 当我 运行 使用 IntelliJ 的
Run XxxTest
处理 属性 的测试时,仅当两个注释都存在时才能正常工作。经过一些调试后,我遇到了 JupiterTestEngine
- 外部启动器(Maven、IntelliJ 等)的 class 即 运行。似乎 IntelliJ 添加了一个 属性 到它的测试启动器:junit.jupiter.conditions.deactivate,这通常很有用——多亏了它,我们甚至可以 运行 甚至在本地使用条件注释禁用的测试,忽略它们。 属性 的值在 @DisabledIfSystemProperty
不存在时为 org.junit.*Enabled*Condition
,在存在时为 org.junit.*Disabled*Condition
- 条件是解决测试禁用状态的 JUnit 扩展。
(2) 中描述的功能通常很有用,但在您的情况下,它看起来好像注释不起作用。它确实有效,但 IntelliJ 只是绕过了它。
我将我的测试从 JUnit 4 迁移到 JUnit 5。一切正常,但我之前注释的翻译:
@IfProfileValue(name = "run.import.tests", values = {"true"})
进入
@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
没有按预期工作。在迁移之前,我运行了我的测试并传递了参数
-Drun.import.tests=true
只有当我通过它时,它们才会运行。使用 Junit 5,即使使用注释 @EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
,即使未设置参数 run.import.tests
,测试也会运行。
我是不是做错了什么?
为了让它工作,必须添加一个“相反”的注释,所以它们看起来像这样:
@EnabledIfSystemProperty(named = "run.import.tests", matches = "true")
@DisabledIfSystemProperty(named = "run.import.tests", matches = "(?!true)")
我已经检查过了,如果未设置 run.import.tests
属性 或设置为 true
以外的任何其他值,测试 class 将被禁用;如果该值设置为 true
- 测试 class 未禁用。
奇怪的是,the documentation of @EnabledIfSystemProperty 指出:
If the specified system property is undefined, the annotated class or method will be disabled.
然而它不能那样工作,它可能是一个错误。我将尝试调试 JUnit classes,如果我在它们的 GitHub 上创建问题,我将在此处 link 它。
我已经检查了代码并对其进行了多次测试 - 这是摘要:
- 当我 运行 使用 Maven (
mvn test
) 进行测试时,注释@EnabledIfSystemProperty
单独工作正常 - 只有当我添加 [=16] 时测试才 运行 =] 参数。在这种情况下不需要@DisabledIfSystemProperty
。 - 当我 运行 使用 IntelliJ 的
Run XxxTest
处理 属性 的测试时,仅当两个注释都存在时才能正常工作。经过一些调试后,我遇到了JupiterTestEngine
- 外部启动器(Maven、IntelliJ 等)的 class 即 运行。似乎 IntelliJ 添加了一个 属性 到它的测试启动器:junit.jupiter.conditions.deactivate,这通常很有用——多亏了它,我们甚至可以 运行 甚至在本地使用条件注释禁用的测试,忽略它们。 属性 的值在@DisabledIfSystemProperty
不存在时为org.junit.*Enabled*Condition
,在存在时为org.junit.*Disabled*Condition
- 条件是解决测试禁用状态的 JUnit 扩展。
(2) 中描述的功能通常很有用,但在您的情况下,它看起来好像注释不起作用。它确实有效,但 IntelliJ 只是绕过了它。