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

后端 未结 2 2040
醉梦人生
醉梦人生 2021-02-04 12:20

I have a potentially long-running test written with scalatest:

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


        
2条回答
  •  耶瑟儿~
    2021-02-04 12:35

    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)) {
        // ...
      }
    }
    

提交回复
热议问题