【问题标题】:ScalaTest: conditionally ignore a WordSpec testScalaTest:有条件地忽略 WordSpec 测试
【发布时间】:2016-12-15 01:30:20
【问题描述】:

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

是否存在一些隐含的巫术?

【问题讨论】:

    标签: scala scalatest


    【解决方案1】:

    参数太多。 无法正确解析隐式字符串。

    两种选择:

    • 在“测试名称”后加一个点

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

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

    【讨论】:

    • 我已经考虑了单个参数集并对其进行了折扣,因为它对测试结构的改变比我想要的要多一点。但我喜欢您在调用ignoreUntil 时使用显式点字符的解决方案。这对我来说似乎是一个不那么侵入性的解决方案。感谢您解释错误并提出解决方案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-13
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 1970-01-01
    相关资源
    最近更新 更多