简体   繁体   English

scalatet 中的 assertEquals

[英]assertEquals in scalatest

I'd like to use something similar to jUnit's assertEquals in scalatest.我想在 scalatest 中使用类似于 jUnit 的assertEquals的东西。

Does the framework implement it or it does just provide assert and I should use assertEquals from jUnit itself?框架是实现它还是只提供assert ,我应该使用 jUnit 本身的assertEquals

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, eg a loop count if testing using a loop.最好使用它来包含一些不明显的数据值,例如,如果使用循环进行测试,则循环计数。

Then there's the ShouldMatchers approach such as然后是 ShouldMatchers 方法,例如

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.然而,学习使用它只是有点困难 - API 中有一些角落和缝隙。 And you can't put in a hint explanation.而且你不能输入提示解释。

One of the great things about ScalaTest is that it doesn't force you to do things its way - it allows you to choose the approach that fits best with your particular situation and preferences. ScalaTest 的一大优点是它不会强迫您按照自己的方式做事 - 它允许您选择最适合您的特定情况和偏好的方法。 As well as the approaches in the other answers you've already received, there's also (and my personal preference):除了您已经收到的其他答案中的方法外,还有(以及我个人的偏好):

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

(note that expectResult is expect in ScalaTest 1.x). (请注意, expectResult在 ScalaTest 1.x 中是expect )。

assert(Actual.equals(Expected))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM