如何跳过specs2中的“should”块/片段?

时间:2013-02-20 04:03:48

标签: scala specs2

假设我在“unit”样式中定义了specs2规范,如下所示:

import org.specs2.mutable

class MyClassSpec extends mutable.Specification {
  "myMethod" should {
    "return positive values" in {
      MyClass.myMethod must beGreaterThan(0)
    }

    "return values less than 100" in {
      MyClass.myMethod must beLessThan(100)
    }
  }
}

是否有一种简单的方法可以跳过/禁用/标记myMethod的should block / fragment中的所有示例?

显然,我可以从块中的每个单独的示例中调用pendingUntilFixed或返回pending,但对于具有许多规范的块来说,这将是相当繁琐的。

如果MyClass.myMethod难以实施并受到惩罚,这似乎很常见。还有另一种方法,这通常在specs2中完成吗?

1 个答案:

答案 0 :(得分:7)

您可以混合使用Tags特征并定义您想要的任何section

import org.specs2.mutable._

class MyClassSpec extends Specification with Tags {

  section("pending")
  "myMethod" should {
    "return positive values" in {
      MyClass.myMethod must beGreaterThan(0)
    }

    "return values less than 100" in {
      MyClass.myMethod must beLessThan(100)
    }
  }
  section("pending")
}

然后使用exclude pending

运行您的规范
>test-only *MyClassSpec* -- exclude pending

记录在案here

您还可以使用隐式上下文来确保should块中的所有示例均为PendingUntilFixed

import org.specs2._
import execute._

class MyClassSpec extends mutable.Specification { 
  "this doesn't work for now" >> {
    implicit val puf = pendingContext("FIXME")
    "ex1" in ko
    "ex2" in ok
  }
  "but this works ok" >> {
    "ex3" in ko // maybe not here ;-)
    "ex4" in ok
  }

  def pendingContext(reason: String) = new mutable.Around {
    def around[T <% Result](t: =>T) = 
      t.pendingUntilFixed(reason)
  }
}

更新specs2 3.x

import org.specs2._
import execute._

class TestMutableSpec extends mutable.Specification {
  "this doesn't work for now" >> {
    implicit def context[T] = pendingContext[T]("FIXME")

    "ex1" in ko
    "ex2" in ok
  }
  "but this works ok" >> {
    "ex3" in ko // maybe not here ;-)
    "ex4" in ok
  }

   def pendingContext[T](reason: String): AsResult[MatchResult[T]] =     
     new AsResult[MatchResult[T]] {
      def asResult(t: =>MatchResult[T]): Result =
        AsResult(t).pendingUntilFixed(reason)
     }
}