问题
Is it common practice to unit test stdIn/stdOut? If so, then how would you test something like this:
import scala.io.StdIn._
object Test {
def main(args: Array[String]) = {
println("Please input your text. Leaving an empty line will indicate end of the input.")
val input = Iterator.continually(readLine()).takeWhile(_ != "").mkString("\n")
val result = doSomethingWithInput(input)
println("Result:")
println(result)
}
}
I'm normally using ScalaTest if that makes any difference.
回答1:
Since Scala uses the standard Java stream(System.out
, System.in
) behind the scenes you can test it, by replacing the standard streams with your custom stream which you can further inspect. See here for more details.
In reality though I'd primarily focus making sure doSomethingWithInput
is fully tested and maybe follow up with testing of the input reading (to make sure the stop condition and input string construction work as expected).
If you've already tested the value that you're going to println
then making sure it has been send to the console stream gives a very little benefit for a lot of effort. Moreover such test cases will be the pain to maintain going forward. As always it depends on your use case, but in majority of cases I'd just refrain from testing it.
回答2:
I would change doSomethingWithInput to take a BufferedSource as parameter so you can write your unit tests with any source stream not just stdin
回答3:
Console object provides withIn
and withOut
methods enabling temporary redirection of stdin and stdout. Here is a working example which tests method vulcanIO
which both reads and prints to stdin/stdout:
import java.io.{ByteArrayOutputStream, StringReader}
import org.scalatest._
import scala.io.StdIn
class HelloSpec extends FlatSpec with Matchers {
def vulcanIO(): Unit = {
println("Welcome to Vulcan. What's your name?")
val name = StdIn.readLine()
println("What planet do you come from?")
val planet = StdIn.readLine()
println(s"Live Long and Prosper 🖖, $name from $planet.")
}
"Vulcan salute" should "include 🖖, name, and planet" in {
val inputStr =
"""|Jean-Luc Picard
|Earth
""".stripMargin
val in = new StringReader(inputStr)
val out = new ByteArrayOutputStream()
Console.withOut(out) {
Console.withIn(in) {
vulcanIO()
}
}
out.toString should (include ("🖖") and include ("Jean-Luc Picard") and include ("Earth"))
}
}
Note how redirection happens inside
Console.withOut(out) {
Console.withIn(in) {
vulcanIO()
}
}
and how we assert on the output stream out
out.toString should (include ("🖖") and include ("Jean-Luc Picard") and include ("Earth"))
来源:https://stackoverflow.com/questions/24227932/scala-unit-testing-stdin-stdout