ScalaTest: conditionally ignore a WordSpec test

1.3k Views Asked by At

ScalaTest WordSpec allows tests to be ignored like this:

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

Which is great, but I don't want to forget about ignored tests. So I would like the ignore behaviour to expire after a provided date. At which point the test would run as normal and either: 1) pass (hopefully) or 2) remind me that it is still broken.

To achieve this I'm trying to extend the WordSpec DSL to support an ignoreUntil function. This would accept a string expiry date and ignore the test if the date still in the future, else run the test.

My test spec would then look like:

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

I've implemented the ignoreUntil function here:

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 = ???
  }
}

However sbt test gives me the following compilation error:

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

Why exactly does the compiler not like the signature of ignoreUntil function?

Is there some voodoo with implicits going on?

1

There are 1 best solutions below

1
On BEST ANSWER

There are too many arguments. Implicit on string was not able to be resolved properly.

Two options:

  • add a dot after "test name"

  • move both expireDate and test: => Any parameters to one paramter set.

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