2016-04-21 72 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())