scalacheck属性集

时间:2015-05-04 09:48:10

标签: scala scalacheck

使用scalacheck,可以定义一些Properties

object MyProps extends Properties("MyProps") {
  property("myProp1") = forAll { (n:Int, m:Int) =>
    n+m == m+n
  }
  property("myProp2") = forAll { ... }
  property("myProp3") = forAll { ... }
}

我想仅覆盖一个属性的默认最小成功测试次数(minSuccessfulTests),例如仅适用于"myProp2"

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试隐式转换为自己的PropFromFun实施,而不是Prop

class PropWithParameters(prop: Prop, params: Test.Parameters) extends Prop {
    def apply(prms: Gen.Parameters) = prop(prms)

    def check(): Unit = {
         // Call super.check with params.
    }

    def withParams(moreParams: Test.Parameters): PropWithParameters = {
        // return a new instance with params merged with moreParams.
    }
}

object PropWithParameters {
    implicit def convert(input: Prop): PropWithParameters = {
        // Initialize with empty params here.

        // I'm not sure if implicits get aligned easily 
        // in case of a subclass.
    }
}

然后你可以做类似的事情:

object MyProps extends Properties("MyProps") {
    property("myProp1") = forAll { (n:Int, m:Int) =>
        n+m == m+n
    }
    property("myProp2") = forAll { ... } withParams(customParams)
    property("myProp3") = forAll { ... }
}