specs2 具有 Before
、After
、Around
等特征,以便能够在安装/拆卸代码中包装示例.
是否有任何东西可以支持为 ScalaCheck 属性的每次“迭代”(即 ScalaCheck 测试的每个值或值集)设置和拆除测试基础设施?
看起来specs2的各种Before、After、Around特征是围绕返回或抛出specs2结果实例而设计的,而Prop不是结果。
最佳答案
此问题现已在最新的 1.12.2-SNAPSHOT 中修复。您现在可以编写:
import org.specs2.ScalaCheck
import org.specs2.mutable.{Around, Specification}
import org.specs2.execute.Result
class TestSpec extends Specification with ScalaCheck {
"test" >> prop { i: Int =>
around(i must be_>(1))
}
val around = new Around {
def around[T <% Result](t: =>T) = {
("testing a new Int").pp
try { t }
finally { "done".pp }
}
}
}
这将在属性的“主体”之前和之后执行代码。
您还可以更进一步,创建一个支持方法来将隐式 around
传递给您的 Props:
class TestSpec extends Specification with ScalaCheck {
"test" >> propAround { i: Int =>
i must be_>(1)
}
// use any implicit "Around" value in scope
def propAround[T, R](f: T => R)
(implicit a: Around,
arb: Arbitrary[T], shrink: Shrink[T],
res: R => Result): Result =
prop((t: T) => a(f(t)))
implicit val around = new Around {
def around[T <% Result](t: =>T) = {
("testing a new Int").pp
try { t }
finally { "done".pp }
}
}
}
关于scala - ScalaCheck 之前/之后/前后?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12461035/