Writing Human-Readable Assertions with Matchers
Learn another way to write test cases in ScalaTest that matches DSL.
Defining matchers
Assertions are low level and don’t enforce any writing style. ScalaTest lets us write more expressive assertions using its matchers. Matchers come in two types, must
and should
. In this lesson and some that follow, we’ll explore the main should
matchers that ScalaTest defines. The must
matchers work exactly the same way. In the end, choosing between should
and must
is a matter of personal preference.
Our TestSuite
definition now looks like this:
Press + to interact
import org.scalatest.flatspec.AnyFlatSpecimport org.scalatest.matchers.should.Matcherstrait TestSuite extends AnyFlatSpec with Matchers
Relational operators and equality matchers
The simplest matchers are probably those that check equality and compare two values. ScalaTest defines five matchers to check equality. In the following list, we use ...
Get hands-on with 1400+ tech skills courses.