《Programming in Scala 3rd》阅读笔记

Chapter 14 《Assertions and Tests

2018-06-22  本文已影响3人  liqing151

查看软件是否符合预期的两个主要工具为断言和测试。


断言

Scala测试
import org.scalatest.FunSuite
import Element.elem
class ElementSuite extends FunSuite {
test("elem result should have passed width") {
val ele = elem('x', 2, 3)
assert(ele.width == 2)
}
}

FunSuite就是风格测试的一个例子。Fun指的是函数,test是定义在FunSuite中的一个方法,被ElementSuite的主构造方法调用,可以在()给出测试的名称,在{}给出具体的测试代码,但test说到底还是一个函数,接收一个String类型参数和一个函数类型参数,函数类型参数是传名参数。


测试报告

作为规格说明的测试
class ElementSpec extends FlatSpec with Matchers {
"A UniformElement" should
"have a width equal to the passed value" in {
val ele = elem('x', 2, 3)
ele.width should be (2)
}
it should "have a height equal to the passed value" in {
val ele = elem('x', 2, 3)
ele.height should be (3)
}
it should "throw an IAE if passed a negative width" in {
an [IllegalArgumentException] should be thrownBy {
elem('x', -2, 3)
}
}
}

规格子句有A UniformElement表示要测试的主体,然后是should,must,canhave a width equal to the passed value为描述主体具有某种行为的字符串,然后是in,在in中编写用于测试指定行为的代码。


基于特质的测试
上一篇 下一篇

猜你喜欢

热点阅读