2016-04-21 70 views
4

首先,我看到它和this other post听起来完全一样,除了一件事情,我不能使用fixture.TestDataFixture,因为我不能扩展fixture.FreeSpecLike,并且我相信一定有一些 (看起来更像这样的代码)没有夹具的ScalaTest测试名称?

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

回答

0

,发现了一个答案(一好没collegue),不知道我喜欢它,但工作原理:

的特质,其他的测试依赖于

class MySpec extends FlatSpecLike { 
//... other stuff 
    var testName = "UndefinedTestName" 
    override def withFixture (test: NoArgTest) :Outcome= { 
     testName = test.name 
     super.withFixture(test) 
    } 
} 

简单的解决方案,而是模糊的,也不知是否有人看到它

1

任何问题,而你已经来到基本上这个解决方案,这里是一个更安全的变化:

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())