How to measure and display the running time of a single test?

你说的曾经没有我的故事 提交于 2019-12-03 00:03:50

the -oD option will give the duration of the test. For example, I use the following in my build.sbt.

testOptions in Test += Tests.Argument("-oD")

EDIT:

You can also use the following for individual runs:

> test-only org.acme.RedSuite -- -oD

See http://www.scalatest.org/user_guide/using_scalatest_with_sbt.

Moreover, you can define the following function for general time measurements:

def time[T](str: String)(thunk: => T): T = {
  print(str + "... ")
  val t1 = System.currentTimeMillis
  val x = thunk
  val t2 = System.currentTimeMillis
  println((t2 - t1) + " msecs")
  x
}

and use it anywhere (not dependent on ScalaTest)

test("a long running test") {
  time("test running"){
    failAfter(Span(60, Seconds)) {
    // ...
  }
}

In addition to r.v.'s answer: If you have multiproject builds the testOptions in Test += Tests.Argument("-oD") does not work on root level in build.sbt, because Test refers to src/test/scala. You have to put it inside your Sub-Project settings

Project(id = "da_project", base = file("da_project"))
    .settings(
        testOptions in Test += Tests.Argument("-oDG"),
        libraryDependencies ++= Seq(
            ...
        )
    )
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!