如何将 Specs2 与 Scalacheck 结合使用来自动测试字符串参数?
重写的 Scala specs2 测试框架将自动化测试与 scalacheck。 Specs2 文档中给出的示例介绍了如何将 scalacheck 与 specs2 一起使用 使用整数或更复杂的自定义生成器,如eric 的 json 示例。
在尝试让一个不太复杂的示例工作时,我很挣扎,因为如果我想生成字符串参数而不是整数,我不知道如何使用specs2和scalacheck。此快速入门示例对象 StringSpecification 如何
import org.scalacheck._
扩展 Properties("String") {
属性(“startsWith”)= Prop.forAll((a:字符串,b:字符串)
=> (a+b).startsWith(a))
property("endsWith") = Prop.forAll((a: String, b: String)
=> (a+b).endsWith(b))
// 这真的总是这样吗?
property("concat") = Prop.forAll((a: String, b: String) =>;
(a+b).长度> a.长度&& (a+b).长度> b.长度
)
property("substring") = Prop.forAll((a: String, b: String) =>;
(a+b).substring(a.length) == b
)
property("substring") = Prop.forAll((a: String, b: String, c: String) =>
(a+b+c).substring(a.length, a.length+b.length) == b
)
}
取自 scalacheck 主页,看看它是否是使用 scalacheck 集成编写为 Specs2 规范的?
The rewritten specs2 testing framework for Scala integrates automated testing with scalacheck. The examples given in the specs2 documentation on how to use scalacheck together with specs2 either use integers or more complicated custom generators as in eric's json example.
While trying to get a slightly less complicated example working, I'm struggling because I don't know how one would use specs2 and scalacheck if I want to generate String arguments instead of Integers. How would this Quickstart example
import org.scalacheck._
object StringSpecification extends Properties("String") { property("startsWith") = Prop.forAll((a: String, b: String) => (a+b).startsWith(a))
property("endsWith") = Prop.forAll((a: String, b: String) => (a+b).endsWith(b))
// Is this really always true? property("concat") = Prop.forAll((a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length )
property("substring") = Prop.forAll((a: String, b: String) => (a+b).substring(a.length) == b )
property("substring") = Prop.forAll((a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b ) }
taken from the scalacheck homepage look, if it was written as Specs2 specification using the scalacheck integration?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个非常直接的翻译是使用
check
方法和简单的函数:输出实际上显示
concat
属性不正确:Eric。
A very direct translation is using the
check
method and simple functions:And the output actually shows that the
concat
property is not correct:Eric.
有关在specs2中使用ScalaCheck库的更多信息,请查看specs2 文档中的 ScalaCheck 指南。
For more information on using the ScalaCheck library in specs2, check out the ScalaCheck Guide in the specs2 documentation.