我想做点什么:
class MySpec extends Specification with BeforeAfterExample {
var testName
var clientDir
def before {
testName = fragmentName.replaceAll(" ", "-")
clientDir = new File(workspaceRoot, testName)
clientDir.mkdirs()
}
def after {
FileUtils.deleteDirectory(clientDir)
}
}
答案 0 :(得分:0)
这可能有用:
class MySpec extends Specification with BeforeAfterExample {
var currentExample = 0
var testName = ""
var clientDir:File = null
def before {
testName = is.examples(currentExample).desc.toString.replaceAll(" ", "-")
clientDir = new File(workspaceRoot, testName)
clientDir.mkdirs()
}
def after {
FileUtils.deleteDirectory(clientDir)
currentExample += 1
}
}
我不认为你可以在之前和之后的方法中获得更多的上下文,而不会像这样做一些hacky。
答案 1 :(得分:0)
您可以使用specs2 ExampleFactory
import org.specs2._
import specification._
class TestSpec extends Specification { def is =
"test" ! {
ok
}
case class BeforeAfterExample(e: Example) extends BeforeAfter {
def before = println("before "+e.desc)
def after = println("after "+e.desc)
}
override def exampleFactory = new ExampleFactory {
def newExample(e: Example) = {
val context = BeforeAfterExample(e)
e.copy(body = () => context(e.body()))
}
}
}
API的这一部分最近才刚刚开放,所以它现在仅在1.15-SNAPSHOT中可用(你可以通过直接在{{1}开头的软件包中创建工厂来解决这个限制,使用更新的specs2版本})。
答案 2 :(得分:0)
有点hacky,但这对我有用:
/**
* Returns the name of the currently executing example
*/
def getCurrentExampleName(spec: Specification): String = {
val stack = new Exception().getStackTrace
val specLinesUpStack = for (
line <- stack
if line.getClassName.startsWith(spec.getClass.getName))
yield line.getLineNumber
spec.is.examples
.find(e => specLinesUpStack.contains(e.location.lineNumber))
.get
.desc.toString()
}