Spock 框架包含 @Unroll 注释,这导致将参数化测试中的每个“案例”显示为单独的测试。 ScalaTest 是否有类似的可能?

最佳答案

最接近的是 table-driven property checks :

import org.scalatest.prop.TableDrivenPropertyChecks._

val fractions =
  Table(
    ("n", "d"),  // First tuple defines column names
    (  1,   2),  // Subsequent tuples define the data
    ( -1,   2),
    (  1,  -2),
    ( -1,  -2),
    (  3,   1),
    ( -3,   1),
    ( -3,   0),
    (  3,  -1),
    (  3,  Integer.MIN_VALUE),
    (Integer.MIN_VALUE, 3),
    ( -3,  -1)
  )

import org.scalatest.matchers.ShouldMatchers._

forAll (fractions) { (n: Int, d: Int) =>

  whenever (d != 0 && d != Integer.MIN_VALUE
      && n != Integer.MIN_VALUE) {

    val f = new Fraction(n, d)

    if (n < 0 && d < 0 || n > 0 && d > 0)
      f.numer should be > 0
    else if (n != 0)
      f.numer should be < 0
    else
      f.numer should be === 0

    f.denom should be > 0
  }
}

还有其他技术,例如 "sharing tests"
更多 property-based testing

关于scala - Spock 参数化与 Scala TableDrivenPropertyChecks,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31215911/

10-12 23:47