Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

assertEquals in scalatest

Tags:

testing

scala

I'd like to use something similar to jUnit's assertEquals in scalatest.

Does the framework implement it or it does just provide assert and I should use assertEquals from jUnit itself?

like image 928
mariosangiorgio Avatar asked Oct 15 '12 02:10

mariosangiorgio


People also ask

Should VS must ScalaTest?

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).

What is ScalaTest used for?

Overview. ScalaTest is one of the most popular, complete and easy-to-use testing frameworks in the Scala ecosystem. Where ScalaTest differs from other testing tools is its ability to support a number of different testing styles such as XUnit and BDD out of the box.

What is a fixture in ScalaTest?

Regarding to the ScalaTest documentation: A test fixture is composed of the objects and other artifacts (files, sockets, database connections, etc.) tests use to do their work. When multiple tests need to work with the same fixtures, it is important to try and avoid duplicating the fixture code across those tests.


1 Answers

There's 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-equals, which gives much better error messages than double-equals when the test fails. Also, the second case provides a hint to be printed if the test fails. It's best to use this for including some data value that would not otherwise be obvious, e.g. a loop count if testing using a loop.

Then there's the ShouldMatchers approach such as

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

This is often preferred because it reads easily. However, learning to use it is just a bit harder - there are some nooks and crannies in the API. And you can't put in a hint explanation.

like image 106
Rick-777 Avatar answered Oct 19 '22 19:10

Rick-777