Suppressing test output in SBT

I’m trying to get a very, very verbose SBT build to produce a lot less output on the console.

The verbosity comes from running lots of tests that write to standard output. It’s handy having all this output when running locally, but I suspect it is killing a downstream automated build, one that does not make its logs available.

The output is predominantly from calls to println, not from logging.

I’m loath to cutover all the println calls, so I tried modifying the SBT build file to suppress output on forked tests, using Test / forkOptions. The way I’ve done it is overblown, but I’m trying to get this to work - and failing.

An excerpt from the SBT build file:

  Test / testGrouping := {
    val tests = (Test / definedTests).value

    tests
      .groupBy(_.name)
      .map { case (groupName, group) =>
        new Group(
          groupName,
          group,
          SubProcess(
            (Test / forkOptions).value
              .withRunJVMOptions(
                Vector(
                  s"-Dtrials.runDatabase=trialsRunDatabaseForGroup$groupName"
                )
              )
              // Redirect output to a null output stream.
              .withOutputStrategy(
                OutputStrategy.CustomOutput(OutputStream.nullOutputStream)
              )
          )
        )
      }
      .toSeq
  },
  Global / concurrentRestrictions := Seq(Tags.limit(Tags.ForkedTestGroup, 6)),
  Test / fork                     := true,
  Test / testForkedParallel       := false,
 // Once more for luck - redirect output to a null output stream.
  Test / forkOptions := (Test / forkOptions).value.withOutputStrategy(
    OutputStrategy.CustomOutput(OutputStream.nullOutputStream)
  ),

Note the calls to withOutputStrategy( OutputStrategy.CustomOutput(OutputStream.nullOutputStream) )

Still it pumps out garbage!

If push comes to shove, then yes, I’ll cut over to logging, but is there a way this small change can be made to work?

Hmm,

It seems this approach does work, but not for the JUnit5 tests executed by the SBT build, only those written with Scalatest.

Could be something to do with test grouping, or maybe the SBT JUnit5 integration doesn’t honour the output stream redirection?

Perhaps cutting over to logging isn’t such a bad idea after all.

EDIT: It turns out that the JUnit5 integration supports a quiet option: testOptions += Tests.Argument(jupiterTestFramework, "-q"),.

Where there’s a will, there’s a way.