By Alvin Alexander. Last updated: April 22, 2024
When using ScalaTest, Scala, and SBT, and you want to print output in a ScalaTest unit test that you can see, such as printing to STDOUT with println
, I just found out that I can do it using its info
function, like this:
test("hello world") { info("HELLO WORLD") assert(1 == 1) }
Now when you run your ScalaTest unit tests with SBT, you’ll see that HELLO WORLD
output mixed in with your other ScalaTest output.
As more of a real-world need for this, I just used this technique to print the value of System.getProperty("user.dir")
from inside a ScalaTest unit test, so I could be sure where my tests were running.
So while println
and System.err.println
don’t work for printing output to STDOUT or STDERR, this ScalaTest info
function does print output, which is nice.