2

I am using ScalaTest and have a test suite that contains many tests, and each test can take about a minute to run.

class LargeSuite extends FunSuite {
    test("Name of test 1") { ... }
    ...
    test("Name of test n") { ... }
}

As is usual with running ScalaTest tests from the SBT console, nothing is reported to the screen until each test in the FunSuite has been run. The problem is that when n is large and each test is slow to run, you do not know what is happening. (Running top or Windows Task Manager and looking for the CPU usage of Java is not really satisfactory.)

But the real problem is when the build is run by Travis CI: Travis assumes that the build has gone wrong and kills it if 10 minutes pass and nothing is printed to the screen. In my case, Travis is killing my build, even though the tests are still running, and each individual test in a FunSuite does not require 10 minutes (although the entire suite does require more than 10 minutes because of the number of tests).

My first question is therefore this: how can I get ScalaTest to report on progress to the console after each test in FunSuite, in an idiomatic way?

My partial solution is to use the following trait as a mixin, which solves the problem with Travis:

trait ProgressConsolePrinter extends BeforeAndAfterEach with BeforeAndAfterAll {
  self: Suite =>

  override def beforeAll: Unit = {
    Console.print(s"$suiteName running")
    Console.flush
  }

  override def afterEach: Unit = {
    Console.print(".")
    Console.flush
  }

  override def afterAll: Unit = {
    Console.println
    Console.flush
  }
}

But I understand that using Console to print to the SBT console is not entirely reliable (Googling this seems to somewhat confirm this from the experiences of others).

Also (i) anything you print from Console does not go via SBT's logger, and is therefore not prefixed with [info], and (ii) when trying the above, the messages printed from Console are jumbled up with other messages from SBT. If I was able to use the proper logger, then neither of these things would happen.

My second question is therefore this: How can I print to an SBT logger from within a test in ScalaTest?

Andrew Bate
  • 406
  • 3
  • 17

2 Answers2

4

To get ScalaTest to report to the console after each test within SBT, add logBuffered in Test := false to your build configuration. (See the SBT docs.)

For general logging purposes within SBT, you can use an instance of sbt.util.Logger obtained from streams.value.log within an SBT task as described here. Wilson's answer below can be used if logging is required from the test code.

(I'm answering my own question two years later, but this is currently the first hit on Google for "ScalaTest sbt progress".)

Andrew Bate
  • 406
  • 3
  • 17
0

This may be helpful for your second question.

To log like that you must use scala logging. You must add scala logging as a test dependency, extend some of its logging classes (suggest LazyLogging) and then call logger.info("Your message").

Community
  • 1
  • 1