简体   繁体   中英

How to create new test report directory with timestamp every time I run a test and keep the old test reports using scalatest and sbt

I am using scalatest to run some tests. Now every time I run a test the test results get stored in target/test-reports overwriting the previous test results. I want to store the results in a new folder with timestamp in the folder name. Like in target/test-reports/dd-mm-yy-hhmmss folder and keep the old results intact. How to get the time stamp in build.sbt and use it to make the folder name.

Currently my build.sbt looks like this :

testOptions in Test ++= Seq(Tests.Argument(TestFrameworks.ScalaTest, "-o"), Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/test-reports"))

Please suggest how to get the timestamp and use it in folder name.

Try

libraryDependencies += "org.pegdown" % "pegdown" % "1.6.0",
testOptions in Test ++= Seq(
  Tests.Argument(TestFrameworks.ScalaTest, "-o"), 
  Tests.Argument(TestFrameworks.ScalaTest, "-h", s"target/test-reports-$testDirTimestamp")
)

def testDirTimestamp = {
  import java.time.LocalDateTime
  import java.time.format.DateTimeFormatter
  LocalDateTime.now.format(DateTimeFormatter.ofPattern("yyyy-MM-ddHHmmss"))
}

which after executing sbt test should create reports under

target/test-reports-2019-07-02074159

I have figured it out. We can use variables in buld.sbt and use it to construct the directory name.

val format = new SimpleDateFormat("dd-MM-yy-hhmmss")
val timeStamp : String = format.format(Calendar.getInstance().getTime())
val resultDirectory : String = "target/test-reports/"+timeStamp

testOptions in Test ++= Seq(Tests.Argument(TestFrameworks.ScalaTest, "-o"), Tests.Argument(TestFrameworks.ScalaTest, "-h", resultDirectory))
libraryDependencies +=  "org.pegdown" % "pegdown" % "1.6.0" % "test"

I tried this earlier but did not work. The reason is every time you change the build.sbt file you need to reload the sbt shell, which I did not earlier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM