[英]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 上創建問題,我將在此處鏈接。
我已經瀏覽了代碼並對其進行了多次測試 - 這是摘要:
mvn test
) 運行測試時,注釋@EnabledIfSystemProperty
單獨工作正常 - 僅當我添加-Drun.import.tests=true
參數時才會運行測試。 在這種情況下不需要@DisabledIfSystemProperty
。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.