Serenity BDD:如何循环使用Soft Assertions的步骤

时间:2018-02-19 12:21:36

标签: java serenity-bdd assertj

我需要对数据数组运行测试,我无法在步骤中找到一种软断言的方法,并在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框架。但我的问题是&#34;我的所有数据都是正确的&#34;步骤被正确标记为FAILURE,但所有子步骤&#34;检查值X&#34;在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 mytete.myAwesomeValidator(myDataArray [i],softAssertion);         }

    softAssertion.assertAll();
}

步骤:

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

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

编辑:尝试用我的尝试澄清问题

1 个答案:

答案 0 :(得分:1)

我会尝试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);
}