ScalaTest:有条件地忽略 WordSpec 测试

ScalaTest: conditionally ignore a WordSpec test

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 函数的签名?

是否有隐含的巫术?

争论太多。 隐式字符串无法正确解析。

两个选项:

  • 在"test name"

  • 后加一个点
  • expireDatetest: => Any 参数移动到一个参数集。

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