3

私は次のようなことをしたいと思います:

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)
  }
}
4

3 に答える 3

0

少しハックですが、これは私にとってはうまくいきます:

/**
 * 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()
}
于 2014-02-18T15:39:09.187 に答える
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 でのみ利用できます (. で始まるパッケージでファクトリを直接作成することで、最近の specs2 バージョンでこの制限を回避できますorg.specs2)。

于 2013-04-20T00:49:16.200 に答える