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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6451530/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:07:51  来源:igfitidea点击:

How to show custom failure messages in ScalaTest?

scalascalatestmatcher

提问by Udayakumar Rayala

Does anyone know how to show a custom failure message in ScalaTest?

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

For example:

例如:

NumberOfElements() should equal (5)

Shows the following message when it fails:

失败时显示以下消息:

10 did not equal 5

10 不等于 5

But i want more descriptive message like:

但我想要更多描述性消息,例如:

NumberOfElements should be 5.

NumberOfElements 应该是 5。

回答by Bill Venners

You're the first to ask for such a feature. One way to achieve this is with withClue. Something like:

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

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

That should get you this error message:

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

NumberOfElements: 10 was not equal to 5

NumberOfElements: 10 不等于 5

If you want to control the message completely you can write a custom matcher. Or you could use an assertion, like this:

如果您想完全控制消息,您可以编写自定义匹配器。或者您可以使用断言,如下所示:

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

Can you elaborate on what your use case is? Why is it that 10 did not equal 5 is not up to snuff, and how often have you had this need?

你能详细说明你的用例是什么吗?为什么 10 不等于 5 不符合要求,您多久有这种需求?

Here's the kind of thing you're requesting:

这是您要求的类型:

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)

So this way you can write:

所以你可以这样写:

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

回答by tilde

New way since 2011: Matchersand AppendedCluetraits. Also, for collection sizes, there are some default messages.

自 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
      }
    }
  }
}

Output looks like this:

输出如下所示:

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)

Note that the Listsize message isn't great for lists with long .toStringoutput.

请注意,List大小消息不适用于具有长.toString输出的列表。

See the scaladocfor more information.

有关更多信息,请参阅Scaladoc

回答by Shubham Chaudhary

You can also use withCluewithout importing anything or adding it to the test class:

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

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

This is imported from Assertionsclass: org.scalatest.Assertions#withClue

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