Does Scalatest have any support for assumptions?

旧巷老猫 提交于 2019-12-04 08:02:56

ScalaTest 2.0 (I think) added support for assumptions:

Trait Assertions also provides methods that allow you to cancel a test. You would cancel a test if a resource required by the test was unavailable. For example, if a test requires an external database to be online, and it isn't, the test could be canceled to indicate it was unable to run because of the missing database. Such a test assumes a database is available, and you can use the assume method to indicate this at the beginning of the test, like this:

assume(database.isAvailable)

http://www.scalatest.org/user_guide/using_assertions

Scalacheck, which is often used in combination with scalatest, can do it:

import org.scalacheck._

object XSpecifictaion extends Properties ("X") { 

    property ("sample (a - b)") = Prop.forAll { (a: Int, b: Int) =>  
      (a < b || (a - b >= 0)) }
}

! (a < b) is your assumption, and (a - b >= 0) the real test; you perform the latter only, if the assumption is true, so you negate your assumption and combine it with a shortcut-OR.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!