亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

JUnit 5 @Tag 不能按預期與 @BeforeEach/@AfterEach 一起工作

JUnit 5 @Tag 不能按預期與 @BeforeEach/@AfterEach 一起工作

MYYA 2023-10-13 16:31:18
有一個使用 JUnit 5 開發的簡單數學運算測試套件。我使用兩個標簽@Tag("add")和@Tag("minus")。使用 Maven 和 Surefire 插件,我選擇用于測試的標簽。<plugin>  <artifactId>maven-surefire-plugin</artifactId>  <version>3.0.0-M3</version>  <configuration combine.children="append">    <excludedGroups></excludedGroups>    <groups>add</groups>  </configuration>  <dependencies>    <dependency>      <groupId>org.junit.jupiter</groupId>      <artifactId>junit-jupiter-engine</artifactId>      <version>5.5.2</version>    </dependency>  </dependencies></plugin>當我在終端上執行 時,mvn test它將僅執行 標記的測試,@Tag("add")但由于某種原因將執行@BeforeEach并@AfterEach專門標記為@Tag("minus")@Tag("minus")@BeforeEachvoid beforeEach(TestInfo test) { /* code */ }@Tag("minus")@AfterEachvoid afterEach(TestInfo test) { /* code */ }由于預期的行為不起作用,我什至強制 with <excludedGroups>,從空值更改為<excludedGroups>minus</excludedGroups>on pom.xml,仍然不起作用。pom.xml我失蹤了,有什么問題嗎?@Tag和@BeforeEachplus之間有沖突@AfterEach嗎?
查看完整描述

2 回答

?
嗶嗶one

TA貢獻1854條經驗 獲得超8個贊

@Tag用于測試類和方法。它與所述測試的設置/拆卸無關。

您可以將這些測試拆分為單獨的測試類,也可以利用Nested Test。嵌套測試可能看起來像這樣:

@DisplayName("A stack")

class MathTest {


? ? @Nested

? ? @DisplayName("when new")

? ? @Tag("add")

? ? class Add {


? ? ? ? @BeforeEach

? ? ? ? void setupAdding() {

? ? ? ? ? ? // ...

? ? ? ? }


? ? ? ? @Test

? ? ? ? @DisplayName("can add")

? ? ? ? void canAdd() {

? ? ? ? ? ? // ...

? ? ? ? }


? ? }


? ? @Nested

? ? @DisplayName("minus")

? ? @Tag("minus")

? ? class Minus {


? ? ? ? @BeforeEach

? ? ? ? void setupMinus() {

? ? ? ? ? ? // ...

? ? ? ? }


? ? ? ? @Test

? ? ? ? @DisplayName("can subtract")

? ? ? ? void testMinus() {

? ? ? ? ? ? // ...

? ? ? ? }


? ? }

}


查看完整回答
反對 回復 2023-10-13
?
四季花海

TA貢獻1811條經驗 獲得超5個贊

我認為 @Tag 注釋只是為了按照共同特征對測試進行分類。如果您需要不同的setUp()和teardown(),我想正確的方法是將它們分開在不同的測試類中,而不是使用標簽注釋。

查看完整回答
反對 回復 2023-10-13
  • 2 回答
  • 0 關注
  • 145 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號