书写测试用例一个步骤是书写测试的预期结果。
函数化
Spec2中缺省Specification Trait是函数化的,也就是说Example的Result值为代码中的最后一条语句提供。比如下面的示例,永远不会失败,这是的第一个测试的结果给丢掉了。
- "my example on strings" ! e1 // will never fail!
- def e1 = {
- "hello" must have size(10000) // because this expectation will not be returned,...
- "hello" must startWith("hell")
- }
因此正确的写法为:
- "my example on strings" ! e1 // will fail
- def e1 = "hello" must have size(10000) and
- startWith("hell")
Thrown
上面的函数化需要仔细指明所有的期望,有时你可能觉得这样很麻烦,比如还是用什么的那个不会失败的例子:
- import org.specs2._
- class HelloWorldAcceptanceSpec extends Specification { def is = s2"""
- This is a specification to check the 'Hello world' string
- "my example on strings" $e1
- """
- def e1 = {
- "hello" must have size(10000) // because this expectation will not be returned,...
- "hello" must startWith("hell")
- }
- }
这个例子来执行不会报失败,我们希望在执行?hello? must have size(10000) 报错,不继续执行下面的测试,此时我们可以使用org.specs2.matcher.ThrownExpectations,此时如果将这个Trait混合到定义的规范中,所有没有达到期望值的测试都会抛出FailureException 异常,Example之后的测试也不执行,比如修改后代码:
- import org.specs2._
- import org.specs2.matcher.ThrownExpectations
- class HelloWorldAcceptanceSpec extends Specification with ThrownExpectations { def is = s2"""
- This is a specification to check the 'Hello world' string
- "my example on strings" $e1
- """
- def e1 = {
- "hello" must have size(10000) // because this expectation will not be returned,...
- "hello" must startWith("hell")
- }
- }
这个测试的第一个检测?hello? must have size(10000)失败,整个Example失败,后续的测试也不会执行。