AssertEquals in scalatest

I would like to use something similar to jUnit assertEquals in scalatest.

Does the infrastructure implement or just provide assert , and should I use assertEquals from jUnit itself?

+4
source share
2 answers

The assert approach, such as

 class EqualsTest extends FunSuite { test("equals") { assert(1 === 1) assert(2 === 2, "The reason is obvious") } } 

Note the use of triple equivalents, which gives much better error messages than double equalities when the test fails. In addition, the second case gives a hint for printing if the test fails. It is best to use this to include some data value that would otherwise not be obvious, for example. the number of cycles when testing using a cycle.

Then there is a ShouldMatchers approach, for example

 class EqualsTest extends FunSuite with ShouldMatchers { test("equals") { 1 should be (1) } } 

This is often preferred because it is easy to read. However, learning how to use it is a little harder - there are a few tricks and cracks in the API. And you cannot explain the hint.

+5
source

One of the great things about ScalaTest is that it doesn’t force you to do things your own way β€” it allows you to choose the approach that best suits your specific situation and preferences. In addition to the approaches in other answers that you have already received, there are also (and my personal preferences):

 class EqualsTest extends FunSuite { test("equals") { expectResult(1) { 1 } } } 

(note that expectResult is expect in ScalaTest 1.x).

0
source

Source: https://habr.com/ru/post/1439720/


All Articles