如何将 Specs2 与 Scalacheck 结合使用来自动测试字符串参数?

发布于 2024-11-14 09:00:54 字数 1339 浏览 4 评论 0原文

重写的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无远思近则忧 2024-11-21 09:00:54

一个非常直接的翻译是使用 check 方法和简单的函数:

package test

import org.specs2._
import org.scalacheck._

class ScalaCheckExamples extends Specification with ScalaCheck { def is =

  "startsWith" ! check { (a: String, b: String) => (a+b).startsWith(a) }                                                ^
  "endsWith"   ! check { (a: String, b: String) => (a+b).endsWith(b) }                                                  ^
  "concat"     ! check { (a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length }                 ^
  "substring"  ! check { (a: String, b: String) => (a+b).substring(a.length) == b }                                     ^
  "substring"  ! check { (a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b }     ^
                                                                                                                        end
 }

输出实际上显示 concat 属性不正确:

  [info] + startsWith
  [info] + endsWith
  [error] x concat
  [error]   A counter-example is ['', ''] (after 0 try)
  [error] the value is false
  [error]  (ScalaCheckExamplesSpec.scala:6)
  [info] + substring
  [info] + substring
  [info]
  [info] Total for specification ScalaCheckExamplesSpec
  [info] Finished in 7 seconds, 547 ms
  [info] 5 examples, 401 expectations, 1 failure, 0 error
  [info]

Eric。

A very direct translation is using the check method and simple functions:

package test

import org.specs2._
import org.scalacheck._

class ScalaCheckExamples extends Specification with ScalaCheck { def is =

  "startsWith" ! check { (a: String, b: String) => (a+b).startsWith(a) }                                                ^
  "endsWith"   ! check { (a: String, b: String) => (a+b).endsWith(b) }                                                  ^
  "concat"     ! check { (a: String, b: String) => (a+b).length > a.length && (a+b).length > b.length }                 ^
  "substring"  ! check { (a: String, b: String) => (a+b).substring(a.length) == b }                                     ^
  "substring"  ! check { (a: String, b: String, c: String) => (a+b+c).substring(a.length, a.length+b.length) == b }     ^
                                                                                                                        end
 }

And the output actually shows that the concat property is not correct:

  [info] + startsWith
  [info] + endsWith
  [error] x concat
  [error]   A counter-example is ['', ''] (after 0 try)
  [error] the value is false
  [error]  (ScalaCheckExamplesSpec.scala:6)
  [info] + substring
  [info] + substring
  [info]
  [info] Total for specification ScalaCheckExamplesSpec
  [info] Finished in 7 seconds, 547 ms
  [info] 5 examples, 401 expectations, 1 failure, 0 error
  [info]

Eric.

南…巷孤猫 2024-11-21 09:00:54

有关在specs2中使用ScalaCheck库的更多信息,请查看specs2 文档中的 ScalaCheck 指南

For more information on using the ScalaCheck library in specs2, check out the ScalaCheck Guide in the specs2 documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文