How to Add Custom Descriptions to ScalaTest Assertion Failures
This had me stumped for a while. Here’s a few ways to do it:
Using assert
assert(your Boolean assertion, "your description")
note: assert takes in a Boolean assertion not a matcher assertion.
Example:
assert(Seq("something").size == 2, "- the size should be one")
Example output:
org.scalatest.exceptions.TestFailedException: List("something") had size 1 instead of expected size 2 - the size should be one
Using WithClue
withClue("Your prefix") { your assertion }
Example:
withClue("Sequence size - ") { Seq("something") should have size 2 }
Example output:
org.scalatest.exceptions.TestFailedException: Sequence size - List("something") had size 1 instead of expected size 2
or if you want a suffix, mix in AppendedClues:
class TestSuite extends FlatSpec with Matchers with AppendedClues {
your assertion withClue("your suffix")
}
Example:
class TestSuite extends FlatSpec with Matchers with AppendedClues {
3 should equal(4) withClue("expecting a header row and 3 rows of data")
}
Example output:
org.scalatest.exceptions.TestFailedException: 3 did not equal 4 expecting a header row and 3 rows of data