简体   繁体   English

每次运行测试时如何使用时间戳创建新的测试报告目录并使用 scalatest 和 sbt 保留旧的测试报告

[英]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.我正在使用 scalatest 运行一些测试。 Now every time I run a test the test results get stored in target/test-reports overwriting the previous test results.现在每次我运行测试时,测试结果都会存储在target/test-reports覆盖之前的测试结果。 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.就像在target/test-reports/dd-mm-yy-hhmmss文件夹中一样,并保持旧的结果不变。 How to get the time stamp in build.sbt and use it to make the folder name.如何在 build.sbt 中获取时间戳并使用它来制作文件夹名称。

Currently my build.sbt looks like this :目前我的 build.sbt 看起来像这样:

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执行sbt test应该在下面创建报告

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.我们可以在 buld.sbt 中使用变量并使用它来构造目录名称。

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.原因是每次更改 build.sbt 文件时都需要重新加载 sbt shell,而我之前没有这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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