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

Viewed 205

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.

2 Answers

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.

Related