30

Just started using ScalaTest and I quite like it.

By just reading the docs I have thus far been unable to figure out whether there is any substantial difference between the can, should and must clauses for a FlatSpec.

In particular, I'm wondering whether a must failure is treated any differently from a should one - or it's just "syntactic sugar" to make the tests better self-documented.

Marco Massenzio
  • 2,822
  • 1
  • 25
  • 37

1 Answers1

26

should and must are the same semantically. But it's not about better documentation, it's basically just down to personal stylistic preference (I prefer must for example).

can is a little different. You can't (nomen omen) use it directly as a matcher, it's only available in a test descriptor. Quote from FlatSpec:

Note: you can use must or can as well as should in a FlatSpec. For example, instead of it should "pop..., you could write it must "pop... or it can "pop....

(the same applies for WordSpec and the two corresponding fixture classes)

Note that for a short time (in ScalaTest 2.0.x I think), the use of must was deprecated, however, in 2.1.0, the decision has been reverted:

Resurrected MustMatchers in package org.scalatest. Changed deprecation warning for org.scalatest.matchers.MustMatchers to suggest using org.scalatest.MustMatchers instead of org.scalatest.Matchers, which was the suggestion in 2.0. Apologies to must users who migrated to should already when upgrading to 2.0.

mikołak
  • 9,605
  • 1
  • 48
  • 70
  • 2
    Thanks for this, appreciated. It would be nice if such a paragraph were to appear, for example, in the Getting Started guide. (the origin of my confusion was probably due to my being familiar with RFC's documentation, where SHOULD and MUST are semantically different). – Marco Massenzio Mar 08 '14 at 15:51
  • @Marco : you're welcome :). Yeah, it can certainly be confusing at first (even 'though, in fairness, the documentation consistently sticks with one flavor). There should be some more explicit statement in the docs, I agree. Also, you must remember to consistently use one or the other - mixing them in a test case/suite will probably cause confusion. – mikołak Mar 08 '14 at 16:37
  • 4
    I will add a clarification to the documentation. The difference between should and must has confused ScalaTest users before, and that was one reason I attempted to deprecate MustMatchers in 2.0. The other was that I want to add "pure assertions" later, and think they should look different, so I was going to reuse "must" for that. A few users requested to have "must" back suggested using "will" for pure. As Tomato said, should and must do the same thing programatically in ScalaTest, but some users feel they aren't equivalent semantically. I recommend you pick one verb and use it consistently. – Bill Venners Mar 08 '14 at 20:23
  • @BillVenners: thanks for the clarification, and also glad I'm not the first one ;-) - and I wish I knew where to start to thank you enough for ScalaTest... I'm totally loving it! – Marco Massenzio Mar 10 '14 at 01:27