【问题标题】: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每次运行测试时如何使用时间戳创建新的测试报告目录并使用 scalatest 和 sbt 保留旧的测试报告
【发布时间】:2019-11-12 17:25:59
【问题描述】:

我正在使用 scalatest 运行一些测试。现在,每次我运行测试时,测试结果都会存储在 target/test-reports 中,覆盖之前的测试结果。我想将结果存储在文件夹名称中带有时间戳的新文件夹中。就像在target/test-reports/dd-mm-yy-hhmmss 文件夹中一样,并保持旧结果不变。如何在 build.sbt 中获取时间戳并使用它来命名文件夹。

目前我的 build.sbt 看起来像这样:

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

请建议如何获取时间戳并在文件夹名称中使用它。

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    试试

    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"))
    }
    

    在执行sbt test 后应该在下面创建报告

    target/test-reports-2019-07-02074159
    

    【讨论】:

    • 谢谢,我明白了。我尝试了类似的事情,但没有奏效。然后我发现我需要重新加载 sbt shell。
    • 只要您使用def 而不是val,就不必重新加载。
    【解决方案2】:

    我想通了。我们可以使用 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"
    

    我之前尝试过,但没有成功。原因是每次更改 build.sbt 文件时都需要重新加载 sbt shell,而我之前没有这样做。

    【讨论】:

      猜你喜欢
      • 2011-08-01
      • 2015-07-07
      • 2013-02-06
      • 1970-01-01
      • 2015-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多