如何测试 Scala 函数?
How can I test a scala function?
我开始学习scala,为了学习,我想玩一些功能。但是,我不知道如何使函数 return 值出现在控制台上。我正在使用 sbt。
我试过 return 和 Console.println,我想我没用对。
例如:
def func(ls: List[Boolean]): Boolean = ls match
{
case Nil => false
case l::ls => l != func(ls)
}
如何查看此功能 returns?
你可以试试这个:
scala> :paste
// Entering paste mode (ctrl-D to finish)
def func(ls: List[Boolean]): Boolean = ls match
{
case Nil => false
case l::ls => l != func(ls)
}
// Exiting paste mode, now interpreting.
func: (ls: List[Boolean])Boolean
scala> func(List(true, false, true))
res0: Boolean = false
Scastie 是 Scala 的在线交互式游乐场,是一种快速入门的方法。例如,在编辑器中粘贴以下内容并按下 Save
按钮
def func(ls: List[Boolean]): Boolean = ls match {
case Nil => false
case l::ls => l != func(ls)
}
func(List(true, false, true))
应该评估 func
并像这样内联显示结果
func(List(true, false, true)) // false: Boolean
也试试println(func(List(true, false, true)))
无需打印即可测试预期结果的另一种方法是使用像这样的断言
assert(func(List(true, false, true)) == false)
要将上述断言转换为真正的单元测试,我们可以从 Scala Giter8 template 实例化一个应用程序,就像这样
sbt new scala/scala-seed.g8
它设置了快速 运行 和测试应用程序所需的所有功能。然后像这样将 func
添加到 src/main/scala/example/Hello.scala
object Hello extends App {
def func(ls: List[Boolean]): Boolean = ls match {
case Nil => false
case l::ls => l != func(ls)
}
}
并像这样
添加相应的单元测试到src/test/scala/example/HelloSpec.scala
class HelloSpec extends FlatSpec with Matchers {
"func" should "return false on List(true, false, true)" in {
Hello.func(List(true, false, true)) shouldEqual false
}
it should "return false on empty list" in {
Hello.func(List()) shouldEqual false
}
// add further tests here
}
现在执行sbt test
应该输出
[info] HelloSpec:
[info] func
[info] - should return false on List(true, false, true)
[info] - should return false on empty list
[info] Run completed in 127 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
我开始学习scala,为了学习,我想玩一些功能。但是,我不知道如何使函数 return 值出现在控制台上。我正在使用 sbt。
我试过 return 和 Console.println,我想我没用对。
例如:
def func(ls: List[Boolean]): Boolean = ls match
{
case Nil => false
case l::ls => l != func(ls)
}
如何查看此功能 returns?
你可以试试这个:
scala> :paste
// Entering paste mode (ctrl-D to finish)
def func(ls: List[Boolean]): Boolean = ls match
{
case Nil => false
case l::ls => l != func(ls)
}
// Exiting paste mode, now interpreting.
func: (ls: List[Boolean])Boolean
scala> func(List(true, false, true))
res0: Boolean = false
Scastie 是 Scala 的在线交互式游乐场,是一种快速入门的方法。例如,在编辑器中粘贴以下内容并按下 Save
按钮
def func(ls: List[Boolean]): Boolean = ls match {
case Nil => false
case l::ls => l != func(ls)
}
func(List(true, false, true))
应该评估 func
并像这样内联显示结果
func(List(true, false, true)) // false: Boolean
也试试println(func(List(true, false, true)))
无需打印即可测试预期结果的另一种方法是使用像这样的断言
assert(func(List(true, false, true)) == false)
要将上述断言转换为真正的单元测试,我们可以从 Scala Giter8 template 实例化一个应用程序,就像这样
sbt new scala/scala-seed.g8
它设置了快速 运行 和测试应用程序所需的所有功能。然后像这样将 func
添加到 src/main/scala/example/Hello.scala
object Hello extends App {
def func(ls: List[Boolean]): Boolean = ls match {
case Nil => false
case l::ls => l != func(ls)
}
}
并像这样
添加相应的单元测试到src/test/scala/example/HelloSpec.scala
class HelloSpec extends FlatSpec with Matchers {
"func" should "return false on List(true, false, true)" in {
Hello.func(List(true, false, true)) shouldEqual false
}
it should "return false on empty list" in {
Hello.func(List()) shouldEqual false
}
// add further tests here
}
现在执行sbt test
应该输出
[info] HelloSpec:
[info] func
[info] - should return false on List(true, false, true)
[info] - should return false on empty list
[info] Run completed in 127 milliseconds.
[info] Total number of tests run: 2
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 2, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.