Scalatest

编程入门 行业动态 更新时间:2024-10-19 11:55:37
本文介绍了Scalatest - 如何测试 println的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Scalatest 中有什么东西可以让我通过 println 语句测试标准输出的输出吗?

Is there something in Scalatest that will allow me to test the output to the standard out via a println statement?

到目前为止,我主要使用 FunSuite with ShouldMatchers.

So far I've mainly been using FunSuite with ShouldMatchers.

例如我们如何检查

object Hi { def hello() { println("hello world") } }

推荐答案

在控制台上测试打印语句的常用方法是稍微不同地构建您的程序,以便您可以拦截这些语句.例如,您可以引入 Output 特征:

The usual way to test print statements on the console is to structure your program a bit differently so that you can intercept those statements. You can for example introduce an Output trait:

trait Output { def print(s: String) = Console.println(s) } class Hi extends Output { def hello() = print("hello world") }

并且在您的测试中,您可以定义另一个特征 MockOutput 实际上拦截调用:

And in your tests you can define another trait MockOutput actually intercepting the calls:

trait MockOutput extends Output { var messages: Seq[String] = Seq() override def print(s: String) = messages = messages :+ s } val hi = new Hi with MockOutput hi.hello() hi.messages should contain("hello world")

更多推荐

Scalatest

本文发布于:2023-11-25 15:25:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630288.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Scalatest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!