ScalaCheck中forAll代码体未执行问题求助
问题:ScalaCheck测试未执行内部代码块,仅输出"begin"
我编写了一段测试ResizableRectangle表面积的Scala代码,通过ScalaCheck生成随机整数作为矩形的坐标和尺寸参数。运行测试后仅打印出"begin",从未打印"inner body",但我预期会多次输出该内容。请问我哪里出错或遗漏了?
原代码如下:
package de.thm.move.shapes import de.thm.move.views.shapes.ResizableRectangle import org.scalacheck.* import de.thm.move.MoveSpec class ResizableRectangleTest extends MoveSpec { "Rectangle area procedure" should "give the correct area" in{ val intPositiveDomain = Gen.choose[Int](1, 10000) //for the coordinates of the shape val intDomain = Gen.choose[Int](-10000, 10000) //for the size of the shape println("begin") //will test for random values for the coordinates and the size of the shape val popRectangle = Prop.forAll(intDomain,intDomain,intPositiveDomain,intPositiveDomain) { (x: Int, y: Int, width: Int, height: Int) => val rectangle: ResizableRectangle = new ResizableRectangle((x.toDouble, y.toDouble), width.toDouble, height.toDouble) val correctSurface: Double = width.toDouble * height.toDouble println("inner body") rectangle.getSurfaceArea() == correctSurface } } }
问题原因与修复方案
你只定义了ScalaCheck的Prop属性实例,但没有触发测试执行。Prop.forAll只是创建了一个待测试的属性对象,不会自动运行测试用例,必须显式执行才能让ScalaCheck生成随机参数并运行内部代码。
方案1:手动调用check()方法
直接在属性定义后调用check()方法,触发测试执行:
package de.thm.move.shapes import de.thm.move.views.shapes.ResizableRectangle import org.scalacheck.* import de.thm.move.MoveSpec class ResizableRectangleTest extends MoveSpec { "Rectangle area procedure" should "give the correct area" in{ val intPositiveDomain = Gen.choose[Int](1, 10000) //坐标参数 val intDomain = Gen.choose[Int](-10000, 10000) //尺寸参数 println("begin") val popRectangle = Prop.forAll(intDomain,intDomain,intPositiveDomain,intPositiveDomain) { (x: Int, y: Int, width: Int, height: Int) => val rectangle: ResizableRectangle = new ResizableRectangle((x.toDouble, y.toDouble), width.toDouble, height.toDouble) val correctSurface: Double = width.toDouble * height.toDouble println("inner body") rectangle.getSurfaceArea() == correctSurface } // 触发测试执行 popRectangle.check() } }
方案2:使用ScalaTest集成的ScalaCheck支持(推荐)
由于你的测试类继承了MoveSpec(基于ScalaTest),更规范的做法是导入ScalaTest与ScalaCheck的集成特质,直接用集成的forAll自动执行测试:
package de.thm.move.shapes import de.thm.move.views.shapes.ResizableRectangle import org.scalacheck.Gen import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks import de.thm.move.MoveSpec class ResizableRectangleTest extends MoveSpec with ScalaCheckPropertyChecks { "Rectangle area procedure" should "give the correct area" in { val intPositiveDomain = Gen.choose[Int](1, 10000) //坐标参数 val intDomain = Gen.choose[Int](-10000, 10000) //尺寸参数 println("begin") // 集成式forAll会自动执行测试,无需手动调用check forAll(intDomain, intDomain, intPositiveDomain, intPositiveDomain) { (x: Int, y: Int, width: Int, height: Int) => val rectangle: ResizableRectangle = new ResizableRectangle((x.toDouble, y.toDouble), width.toDouble, height.toDouble) val correctSurface: Double = width.toDouble * height.toDouble println("inner body") rectangle.getSurfaceArea() == correctSurface } } }
两种方案都能让ScalaCheck生成随机参数并执行内部代码,你会看到多次输出"inner body"。
内容的提问来源于stack exchange,提问作者Daaninator
相关产品推荐
相关产品推荐

