Scala Specs2 测试入门教程(4): 期望结果

jerry Scala 2015年11月25日 收藏

书写测试用例一个步骤是书写测试的预期结果。

函数化

Spec2中缺省Specification Trait是函数化的,也就是说Example的Result值为代码中的最后一条语句提供。比如下面的示例,永远不会失败,这是的第一个测试的结果给丢掉了。

  1. "my example on strings" ! e1 // will never fail!
  2.  
  3. def e1 = {
  4. "hello" must have size(10000) // because this expectation will not be returned,...
  5. "hello" must startWith("hell")
  6. }

因此正确的写法为:

  1. "my example on strings" ! e1 // will fail
  2.  
  3. def e1 = "hello" must have size(10000) and
  4. startWith("hell")

Thrown
上面的函数化需要仔细指明所有的期望,有时你可能觉得这样很麻烦,比如还是用什么的那个不会失败的例子:

  1. import org.specs2._
  2.  
  3.  
  4. class HelloWorldAcceptanceSpec extends Specification { def is = s2"""
  5.  
  6. This is a specification to check the 'Hello world' string
  7.  
  8. "my example on strings" $e1
  9. """
  10.  
  11. def e1 = {
  12. "hello" must have size(10000) // because this expectation will not be returned,...
  13. "hello" must startWith("hell")
  14. }
  15. }

这个例子来执行不会报失败,我们希望在执行?hello? must have size(10000) 报错,不继续执行下面的测试,此时我们可以使用org.specs2.matcher.ThrownExpectations,此时如果将这个Trait混合到定义的规范中,所有没有达到期望值的测试都会抛出FailureException 异常,Example之后的测试也不执行,比如修改后代码:

  1. import org.specs2._
  2. import org.specs2.matcher.ThrownExpectations
  3.  
  4. class HelloWorldAcceptanceSpec extends Specification with ThrownExpectations { def is = s2"""
  5.  
  6. This is a specification to check the 'Hello world' string
  7.  
  8. "my example on strings" $e1
  9. """
  10.  
  11. def e1 = {
  12. "hello" must have size(10000) // because this expectation will not be returned,...
  13. "hello" must startWith("hell")
  14. }
  15. }

这个测试的第一个检测?hello? must have size(10000)失败,整个Example失败,后续的测试也不会执行。