ScalaTest:有条件地忽略WordSpec测试

时间:2016-12-14 16:33:45

标签: scala scalatest

ScalaTest WordSpec允许像这样忽略测试:

class MySpec extends WordSpec {
  "spec" should {
    "ignore test" ignore {fail("test should not have run!")}
  }
}

哪个好,但我不想忘记忽略测试。所以我希望忽略行为在提供的日期之后到期。此时测试将正常运行,并且:1)通过(希望)或2)提醒我它仍然坏了。

为实现这一点,我正在尝试扩展WordSpec DSL以支持ignoreUntil功能。这将接受字符串到期日期,如果日期仍在将来,则忽略测试,否则运行测试。

我的测试规范如下:

class MySpec extends EnhancedWordSpec {
  "spec" should {
    "conditionally ignore test" ignoreUntil("2099-12-31") {fail("test should not have run until the next century!")}
  }
}

我在这里实现了ignoreUntil功能:

class EnhancedWordSpec extends WordSpecLike {
  implicit protected def convertToIgnoreUntilWrapper(s: String) = new IgnoreUntilWordSpecStringWrapper(s)

  protected final class IgnoreUntilWordSpecStringWrapper(wrapped: String) {
    // Run test or ignore, depending if expiryDate is in the future
    def ignoreUntil(expiryDate: String)(test: => Any): Unit = ???
  }
}

但是sbt test给出了以下编译错误:

MySpec.scala:3: type mismatch;
[error]  found   : Char
[error]  required: String
[error]         "ignoreUntil" ignoreUntil("2099-12-31"){fail("ignoreUntil should not have run!")}
[error]                                                ^
[error] one error found
[error] (test:compileIncremental) Compilation failed

为什么编译器不喜欢ignoreUntil函数的签名?

是否有一些带有暗示的伏都教?

1 个答案:

答案 0 :(得分:1)

论证太多了。 字符串隐含无法正确解析。

两个选项:

  • 在"测试名称"

  • 之后添加一个点
  • expireDatetest: => Any参数移至一个参数集。

    "conditionally ignore test".ignoreUntil("2099-12-31") {          fail("test should not have run until the next century!") }