2

I need to run tests on an array of datas, and I can't find a way to do a soft assert in my step AND show the error at the correct step in the Serenity Report.

Example code

@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]);
    }
}

And an example step :

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

    //I need a soft assertion here
}

My attempt :

I tried using the assertj framework. But my problem with it is that the "All my datas are correct" step is correctly flagged as a FAILURE, but all the substeps "Checking the value X" are marked as SUCCESSes on Serenity's report.

my test code :

@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();
}

And the step :

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

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

Edit : tried to clarify the problem with my attempt

Yann Merk
  • 23
  • 2
  • 6
  • Not sure if I'm missing the point but you can instantiate `SoftAssertions softly = new SoftAssertions();` above and then iterate the array (like with that for loop) and inside loop call `softly.assertThat ...` and after loop say `softly.assertAll();`. Hope it helps, if not please tell :) – doom4s Feb 19 '18 at 12:45
  • @doom4s It does indeed report that the step "All my datas are correct" failed, but It reports all the sub steps ("checking the value X") as successes, and I would like to know exactly which failed, not just the entire test. – Yann Merk Feb 19 '18 at 13:16

1 Answers1

3

I would try as() to describe the assertion and not introduce a Step to see if it works (I believe it should):

@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);
} 
Joel Costigliola
  • 6,308
  • 27
  • 35
  • Serenity doesn't show in its report those "as" as steps (which is kinda logical but would be cool), but at least it outputs a list which shows exactly where it failed. It's better than what I have right now, thank you – Yann Merk Feb 20 '18 at 08:26