加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

ScalaTest测试名称没有夹具?

发布时间:2020-12-16 08:45:07 所属栏目:安全 来源:网络整理
导读:首先,我看到它和 this other post听起来完全像我需要的东西,除了一件事,我不能使用fixture.TestDataFixture因为我无法扩展fixture.FreeSpecLike,我相信必须有一些方法来获得以一种看起来更像这样的方式测试名称(想象的代码不能编译) class MySpec extends Fl
首先,我看到它和 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())

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读