簡體   English   中英

Serenity BDD:如何使用軟斷言循環步驟

[英]Serenity BDD : How to loop on Steps with Soft Assertions

我需要對一組數據運行測試,但我找不到在我的步驟中執行軟斷言的方法,並在 Serenity Report 的正確步驟中顯示錯誤。

示例代碼

@Then("All my datas are correct")
public void verifyMyDatas(){

    int[] myDataArray = new int[] {1,2,3,4};

    for(int i = 0; i < myDataArray.length; i++){

        mySteps.myAwesomeValidator(myDataArray[i]);
    }
}

和一個示例步驟:

@Step("Checking the value {0}")
public void myAwesomeValidator(int value){

    //I need a soft assertion here
}

我的嘗試:

我嘗試使用 assertj 框架。 但我的問題是“我的所有數據都正確”步驟被正確標記為失敗,但所有子步驟“檢查值 X”在 Serenity 的報告中都被標記為成功。

我的測試代碼:

@Then("All my datas are correct")
public void verifyMyDatas(){

    SoftAssertions softAssertion = new SoftAssertions();

    int[] myDataArray = new int[] {1,2,3,4};

    for(int i = 0; i < myDataArray.length; i++){

my mySteps.myAwesomeValidator(myDataArray[i], softAssertion); }

    softAssertion.assertAll();
}

和步驟:

@Step("Checking the value {0}")
public void myAwesomeValidator(int value, SoftAssertions softAssertion){

    softAssertion.assertThat(value < 3).isTrue();
}

編輯:試圖通過我的嘗試澄清問題

我會嘗試as()來描述斷言,而不是引入Step來查看它是否有效(我相信它應該):

@Then("All my datas are correct")
public void verifyMyDatas(){

  SoftAssertions softAssertion = new SoftAssertions();

  int[] myDataArray = new int[] {1,2,3,4};
  for(int i = 0; i < myDataArray.length; i++) {
    myAwesomeValidator(myDataArray[i], softAssertion); 
  }

  softAssertion.assertAll();
}

public void myAwesomeValidator(int value, SoftAssertions softAssertion){

  // use as() to describe the assertion 
  softAssertion.assertThat(value)
               .as("awesomely validate value %d", value);
               .isLessThan(3);
} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM