如何在ScalaTest中显示自定义失败消息?

jutyujz0  于 5个月前  发布在  Scala
关注(0)|答案(4)|浏览(45)

有人知道如何在ScalaTest中显示自定义失败消息吗?
举例来说:

NumberOfElements() should equal (5)

字符串
失败时显示以下消息:
10不等于5
但我想要更多的描述性信息,如:
NumberOfElements应为5。

baubqpgj

baubqpgj1#

你是第一个要求这样一个功能的人。实现这一点的一种方法是使用withClue。类似于:

withClue("NumberOfElements: ") { NumberOfElements() should be (5) }

字符串
这应该会给你这个错误消息:
NumberOfElements:10不等于5
如果你想完全控制消息,你可以写一个自定义的匹配器。或者你可以使用一个Assert,像这样:

assert(NumberOfElements() == 5, "NumberOfElements should be 5")


你能详细说明一下你的用例是什么吗?为什么10不等于5不符合标准,你多久有一次这样的需求?
这是你的要求

scala> import org.scalatest.matchers.ShouldMatchers._
import org.scalatest.matchers.ShouldMatchers._

scala> withClue ("Hi:") { 1 + 1 should equal (3) }
org.scalatest.TestFailedException: Hi: 2 did not equal 3
at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150)
at org.scalatest.matchers.ShouldMatchers$.newTestFailedException(ShouldMatchers.scala:2331)

scala> class AssertionHolder(f: => Any) {
     |   def withMessage(s: String) {
     |     withClue(s) { f }
     |   }
     | }
defined class AssertionHolder

scala> implicit def convertAssertion(f: => Any) = new AssertionHolder(f)
convertAssertion: (f: => Any)AssertionHolder

scala> { 1 + 1 should equal (3) } withMessage ("Ho:")
org.scalatest.TestFailedException: Ho: 2 did not equal 3
at org.scalatest.matchers.Matchers$class.newTestFailedException(Matchers.scala:150)
at org.scalatest.matchers.ShouldMatchers$.newTestFailedException(ShouldMatchers.scala:2331)


这样你就可以写:

{ NumberOfElements() should be (5) } withMessage ("NumberOfElements:")

xv8emn3q

xv8emn3q2#

自2011年以来的新方法:MatchersAppendedClue 1 traits。此外,对于集合大小,有一些默认消息。

import org.scalatest.{AppendedClues, Matchers, WordSpec}

class SomeTest extends WordSpec with Matchers with AppendedClues {

  "Clues" should {
    "not be appended" when {
      "assertions pass" in {
        "hi" should equal ("hi") withClue "Greetings scala tester!"
      }
    }
    "be appended" when {
      "assertions fail"  in {
        1 + 1 should equal (3) withClue ", not even for large values of 1!"
      }
    }
    "not be needed" when {
      "looking at collection sizes" in {
        val list = List(1, 2, 3)
        list should have size 5
      }
    }
  }
}

字符串
输出如下所示:

SomeTest:
Clues
  should not be appended
  - when assertions pass
  should be appended
  - when assertions fail *** FAILED ***
    2 did not equal 3, not even for large values of 1! (SomeTest.scala:15)
  should not be needed
  - when looking at collection sizes *** FAILED ***
    List(1, 2, 3) had size 3 instead of expected size 5 (SomeTest.scala:21)


请注意,List大小消息不适用于具有长.toString输出的列表。
更多信息请参见scaladoc。
1我猜AppendedClues特质的灵感来自这个问题,接受答案的Bill Venners是这个特质的作者。

aelbi1ox

aelbi1ox3#

您也可以使用withClue,而无需导入任何内容或将其添加到测试类:

withClue(s"Expecting distinct elements: ${elements.toList}") { elements.length shouldBe 3 }

字符串
这是从Assertions类导入的:org.scalatest.Assertions#withClue

bhmjp9jg

bhmjp9jg4#

一些现有的答案都没有提到的事情。

  1. withClue的位置决定了自定义消息的位置。如果写为withClue(<message>) { <assertion> },则message被前置,如果写为{ <assertion> } withClue(<message>),则message被追加。
    1.为了使用withClue作为中缀运算符,必须混合使用org.scalatest.AppendedClues,或者导入org.scalatest.AppendedClues.convertToClueful
  2. Scala 3不允许使用完全字母命名的方法作为中缀,如果没有使用infix关键字定义的话。为了解决这个问题,请将withClue括在反引号中,如下所示:{ <assertion> }withClue(<message>)

相关问题