Spock参数化与Scala TableDrivenPropertyChecks

时间:2015-07-04 01:16:10

标签: scala scalatest

Spock框架包含@Unroll注释,导致显示每个案例'从参数化测试作为单独的测试。 ScalaTest可能有类似的东西吗?

1 个答案:

答案 0 :(得分:-2)

最接近的是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