14

我从 sbt 运行我的 scalatest,并且输出混淆了 - scalatest 打印所有测试运行,并对它们进行注释,并在中间的某个地方打印统计信息:

> test
[info] Compiling 1 Scala source to /home/platon/Tor/scala-dojo-02/target/scala-2.9.1/classes...
[info] FunsWithListsTests:
[info] - should return list of labels
[info] - should return the average rating of games belonging to Zenga
[info] - should return the total ratings of all games
[info] - should return the total ratings of EA games *** FAILED ***
[info]   0 did not equal 170 (FunsWithListsTests.scala:35)
[error] Failed: : Total 8, Failed 5, Errors 0, Passed 3, Skipped 0
[info] - should increase all games rating by 10 *** FAILED ***
[error] Failed tests:
[error]     dojo.FunsWithListsTests
[info]   List() did not equal List(Game(Activision,40), Game(Zenga,70), Game(Zenga,20), Game(EA,70), Game(EA,120)) (FunsWithListsTests.scala:40)
[info] - should decrease all Zenga games rating by 10 *** FAILED ***
[info]   List() did not equal List(Game(Activision,30), Game(Zenga,50), Game(Zenga,0), Game(EA,60), Game(EA,110)) (FunsWithListsTests.scala:45)
[info] - should create function to find Activision games *** FAILED ***
[info]   List(Game(Activision,30), Game(Zenga,60), Game(Zenga,10), Game(EA,60), Game(EA,110)) did not equal List(Game(Activision,30)) (FunsWithListsTests.scala:50)
[info] - should return a List of tuples consisting of game label and game *** FAILED ***
[info]   List() did not equal List((ACTIVISION,Game(Activision,30)), (ZENGA,Game(Zenga,60)), (ZENGA,Game(Zenga,10)), (EA,Game(EA,60)), (EA,Game(EA,110))) (FunsWithListsTests.scala:56)
[error] {file:/home/platon/Tor/scala-dojo-02/}default-940f03/test:test: Tests unsuccessful
[error] Total time: 1 s, completed Mar 20, 2012 9:27:13 AM

看来如果我积累了大量的测试,搜索那些统计数据和失败的测试会变得很痛苦。

有没有办法来解决这个问题?

4

2 回答 2

7

在我看来,原因是 SBT 默认情况下并行执行测试,就像 maven-surefire-plugin 一样。

正如 ScalaTest wiki 中所解释的,这可以通过以下方式解决:

禁用测试的并行执行 默认情况下,sbt 并行运行所有任务。因为每个测试都映射到一个任务,所以默认情况下测试也是并行运行的。要禁用测试的并行执行:

parallelExecution in Test := false
于 2012-05-08T14:40:39.260 回答
0

有同样的问题并通过将日志保存到单独的文件中来解决它。不理想,但对 CI 有好处,尤其是在长时间运行的集成测试的情况下。我是怎么做到的:

  1. 为日志创建一个特殊目录(例如,在 build.sbt 的末尾):

    lazy val logDirectory = taskKey[File]("A directory for logs")
    logDirectory := {
      val r = target.value / "logs"
      IO.createDirectory(r)
      r
    }
    
  2. [可选] 为项目中的摘要信息指定一个文件:

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

    W禁用颜色

  3. 为每个测试创建一个组。每个组都必须在带有特殊参数的分叉 JVM 中运行测试:

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

    注意,我们可以通过our.logging.appenderour.logging.dir告诉 logback,我们将日志保存在哪里

  4. 创建日志配置(test/resources/logback-test.xml):

    <?xml version="1.0" encoding="UTF-8"?>
    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <target>System.out</target>
            <encoder>
                <pattern>%date{yyyy-MM-dd HH:mm:ss} %-5level [%thread] %logger{26} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <file>${our.logging.dir}/test.log</file>
            <append>false</append>
            <encoder>
                <pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread] %logger{26} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="TRACE">
            <appender-ref ref="${our.logging.appender}"/>
        </root>
    </configuration>
    

就这样。

于 2017-11-11T19:27:46.783 回答