How to pass custom information to Reporter in Scala Test?

305 views Asked by At

Our scala test case calls a REST API for ex. creating a user and it checks if the userId is actually created or not by parsing the output response. In case the REST API throws any error, the userId is empty and the customReport shows the event as TestFailed with org.scalatest.exceptions.TestFailedException: "" equaled "" due to the assert condition (assert(userId != ""))

Is there a way that I can pass the response of REST API to the reporter. Please advise.

class CustomReport extends Reporter {


  override def apply(event: Event): Unit = {

}

}
1

There are 1 answers

0
Mario Galic On

Consider providing custom information on failure via clue, for example,

assert(userId != "", myCustomInformation)

or

withClue(myCustomInformation) {
  userId should not be empty
}

where customInformation could be, say,

case class MyCustomInformation(name: String, id: Int)
val myCustomInformation = MyCustomInformation("picard", 42)

Here is a working example

import org.scalatest._

class ClueSpec extends FlatSpec with Matchers {
  case class MyCustomInformation(name: String, id: Int)

  "Tests failures" should "annotated with clues" in {
    withClue(MyCustomInformation("picard", 42)) {
      "" should not be empty
    }
  }
}

which outputs

[info] Tests failures
[info] - should annotated with clues *** FAILED ***
[info]   MyCustomInformation(picard,42) "" was empty (HelloSpec.scala:10)

For an example of custom reporter consider https://stackoverflow.com/a/56790804/5205022