How to show custom failure messages in ScalaTest?

Viewed 15561

Does anyone know how to show a custom failure message in ScalaTest?

For example:

NumberOfElements() should equal (5)

Shows the following message when it fails:

10 did not equal 5

But i want more descriptive message like:

NumberOfElements should be 5.

3 Answers

You can also use withClue without importing anything or adding it to the test class:

withClue(s"Expecting distinct elements: ${elements.toList}") { elements.length shouldBe 3 }

This is imported from Assertions class: org.scalatest.Assertions#withClue

Related