Why does scalatest mix up the output?

前端 未结 2 805
鱼传尺愫
鱼传尺愫 2021-01-17 10:51

I run my scalatest from sbt, and the output gets mixed up - scalatest prints all the test run, and comments to them, and somewhere in the middle it prints the statistics:

2条回答
  •  醉梦人生
    2021-01-17 11:22

    Have the same issue and solved it by saving logs into separate files. Not ideal, but good for CI, especially in case of long-running integration tests. How I did it:

    1. Create a special directory for logs (for example, at the end of build.sbt):

      lazy val logDirectory = taskKey[File]("A directory for logs")
      logDirectory := {
        val r = target.value / "logs"
        IO.createDirectory(r)
        r
      }
      
    2. [Optionally] Specify a file for a summary information in a project:

      testOptions in test += Tests.Argument(
        TestFrameworks.ScalaTest, "-fW", (logDirectory.value / "summary.log").toString
      )
      

      W disables colors

    3. Create a group for each test. Each group must run a test in a forked JVM with special arguments:

      testGrouping in test := testGrouping.value.flatMap { group =>
        group.tests.map { suite =>
          val fileName = {
            // foo.bar.baz.TestSuite -> f.b.b.TestSuite
            val parts = suite.name.split('.')
            (parts.init.map(_.substring(0, 1)) :+ parts.last).mkString(".")
          }
      
          val forkOptions = ForkOptions(
            bootJars = Nil,
            javaHome = javaHome.value,
            connectInput = connectInput.value,
            outputStrategy = outputStrategy.value,
            runJVMOptions = javaOptions.value ++ Seq(
              "-Dour.logging.appender=FILE",
              s"-Dour.logging.dir=${logDirectory.value / fileName}"
            ),
            workingDirectory = Some(baseDirectory.value),
            envVars = envVars.value
          )
      
          group.copy(
            name = suite.name,
            runPolicy = Tests.SubProcess(forkOptions),
            tests = Seq(suite)
          )
        }
      }
      

      Note, we can tell the logback, where we save our logs through our.logging.appender and our.logging.dir

    4. Create a configuration for logging (test/resources/logback-test.xml):

      
      
          
              System.out
              
                  %date{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{26} - %msg%n
              
          
      
          
              ${our.logging.dir}/test.log
              false
              
                  %date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{26} - %msg%n
              
          
      
          
              
          
      
      

    That's all.

提交回复
热议问题