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


86

有谁知道如何在ScalaTest中显示自定义失败消息?

例如:

NumberOfElements() should equal (5)

失败时显示以下消息:

10不等于5

但是我想要更多描述性的信息,例如:

NumberOfElements应该为5。

Answers:


101

您是第一个要求这种功能的人。实现这一目标的一种方法是withClue。就像是:

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

那应该给你这个错误信息:

NumberOfElements:10不等于5

如果要完全控制消息,可以编写一个自定义匹配器。或者您可以使用这样的断言:

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:")

1
在某些情况下,我必须在it()测试中放置多个断言,并且存在多个整数比较。通过查看日志无法确定哪个断言失败。
Udayakumar Rayala 2011年

但是withClue指定它的方式不可读。最后没有指定消息的方法吗?
Udayakumar Rayala 2011年

1
最后,对于匹配器的DSL来说是不可行的,但是您可以编写一种将withClue参数置于相反顺序的方法。我将在示例中添加一个示例。
Bill Venners

12

自2011年以来的新途径:MatchersAppendedClue特质。另外,对于集合大小,有一些默认消息。

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输出的列表,size消息不是很好。

有关更多信息,请参见scaladoc


3

您也可以在withClue不导入任何内容或将其添加到测试类的情况下使用:

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

这是从Assertions类中导入的:org.scalatest.Assertions#withClue

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.