Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show custom failure messages in ScalaTest?

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.

like image 943
Udayakumar Rayala Avatar asked Jun 23 '11 08:06

Udayakumar Rayala


People also ask

What do you use in ScalaTest to see a detailed diagram of error messages when a test fails?

trait Diagrams extends Assertions. Sub-trait of Assertions that overrides assert and assume methods to include a diagram showing the values of expression in the error message when the assertion or assumption fails.

How do you fail a Scala test?

Or, if you want the test to fail with a message, write: fail("I've got a bad feeling about this.")

What is assert function in Scala?

assert is a precondition in Scala that evaluates a boolean expression as true or false. It's generally used to validate the execution of the program during runtime. We can see assert is used commonly in testing the functionality of programs.


1 Answers

You're the first to ask for such a feature. One way to achieve this is with withClue. Something like:

withClue("NumberOfElements: ") { NumberOfElements() should be (5) } 

That should get you this error message:

NumberOfElements: 10 was not equal to 5

If you want to control the message completely you can write a custom matcher. Or you could use an assertion, like this:

assert(NumberOfElements() == 5, "NumberOfElements should be 5") 

Can you elaborate on what your use case is? Why is it that 10 did not equal 5 is not up to snuff, and how often have you had this need?

Here's the kind of thing you're requesting:

scala> import org.scalatest.matchers.ShouldMatchers._ import org.scalatest.matchers.ShouldMatchers._  scala> withClue ("Hi:") { 1 + 1 should equal (3) } org.scalatest.TestFailedException: Hi: 2 did not equal 3 at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150) at org.scalatest.matchers.ShouldMatchers$.newTestFailedException(ShouldMatchers.scala:2331)   scala> class AssertionHolder(f: => Any) {      |   def withMessage(s: String) {      |     withClue(s) { f }      |   }      | } defined class AssertionHolder  scala> implicit def convertAssertion(f: => Any) = new AssertionHolder(f) convertAssertion: (f: => Any)AssertionHolder  scala> { 1 + 1 should equal (3) } withMessage ("Ho:") org.scalatest.TestFailedException: Ho: 2 did not equal 3 at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150) at org.scalatest.matchers.ShouldMatchers$.newTestFailedException(ShouldMatchers.scala:2331) 

So this way you can write:

{ NumberOfElements() should be (5) } withMessage ("NumberOfElements:") 
like image 158
Bill Venners Avatar answered Sep 19 '22 04:09

Bill Venners