ScalaTest测试名称没有夹具?

首先,我看到它和
this other post听起来完全像我需要的东西,除了一件事,我不能使用fixture.TestDataFixture因为我无法扩展fixture.FreeSpecLike,我相信必须有一些方法来获得以一种看起来更像这样的方式测试名称(想象的代码不能编译)

class MySpec extends FlatSpecLike with fixture.TestDataFixture {
  "this technique" - {
     "should work" in { 
      assert(testData.name == "this technique should work")
    }
    "should be easy" in { td =>
      assert(testData.name == "this technique should be easy")
    }
  }
}

有任何想法吗?我简直不敢相信这样的事情是不可能的:D

最佳答案 虽然您基本上已经解决了这个问题,但这里有一个更安全的变体:

private val _currentTestName = new ThreadLocal[String]

override def withFixture(test: NoArgTest) = {
  _currentTestName.set(test.name)
  val outcome = super.withFixture(test)
  _currentTestName.set(null)
  outcome
}

protected def currentTestName: String = {
  val testName = _currentTestName.get()
  assert(testName != null, "currentTestName should only be called in a test")
  testName
}

交替,

protected def currentTestName = Option(_currentTestName.get())
点赞