繁体   English   中英

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 未被禁用。


奇怪的是, @EnabledIfSystemProperty 的文档指出:

如果指定的系统属性未定义,则注释的 class 或方法将被禁用。

然而它不是那样工作的,它可能是一个错误。 我将尝试调试 JUnit 类,如果我在他们的 GitHub 上创建问题,我将在此处链接。


我已经浏览了代码并对其进行了多次测试 - 这是摘要:

  1. 当我使用 Maven ( mvn test ) 运行测试时,注释@EnabledIfSystemProperty单独工作正常 - 仅当我添加-Drun.import.tests=true参数时才会运行测试。 在这种情况下不需要@DisabledIfSystemProperty
  2. 当我使用 IntelliJ 的Run XxxTest运行测试时,只有两个注释都存在时,该属性的处理才能正常工作。 经过一些调试后,我遇到了JupiterTestEngine - 一个由外部启动器(Maven、IntelliJ 等)运行的 class。 似乎 IntelliJ 向它的测试启动器添加了一个属性: junit.jupiter.conditions.deactivate ,这通常很有用 - 由于我们甚至可以在本地运行使用条件注释禁用的测试,而忽略它们。 The value of the property is org.junit.*Enabled*Condition when @DisabledIfSystemProperty is not present and org.junit.*Disabled*Condition when it is - the conditions are JUnit's extensions that resolve disabled state for the test.

(2) 中描述的功能通常很有用,但在您的情况下,它看起来注释不起作用。 它实际上正在工作,但 IntelliJ 只是绕过它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM