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

发布于 2024-11-16 19:26:58 字数 259 浏览 8 评论 0原文

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

例如:

NumberOfElements() should equal (5)

失败时显示以下消息:

10 不等于 5

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

NumberOfElements 应为 5。

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

For example:

NumberOfElements() should equal (5)

Shows the following message when it fails:

10 did not equal 5

But i want more descriptive message like:

NumberOfElements should be 5.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

燃情 2024-11-23 19:26:58

您是第一个要求此类功能的人。实现此目的的一种方法是使用 withClue。类似的东西:

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

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

NumberOfElements: 10 was not equal to 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:")

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

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

That should get you this error message:

NumberOfElements: 10 was not equal to 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?

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:")
落日海湾 2024-11-23 19:26:58

2011 年以来的新方法:MatchersAppishedClue1 特征。此外,对于集合大小,还有一些默认消息。

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 我猜 AppishedClues 特征是受到这个问题的启发,接受的答案的 Bill Venners 是这个特征的作者。

New way since 2011: Matchers and AppendedClue1 traits. Also, for collection sizes, there are some default messages.

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 List size message isn't great for lists with long .toString output.

See the scaladoc for more information.


1 I'm guessing the AppendedClues trait was inspired by this question, Bill Venners of the accepted answer is the author of this trait.

挽袖吟 2024-11-23 19:26:58

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

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

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

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

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

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

抠脚大汉 2024-11-23 19:26:58

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

  1. withClue 的位置决定了自定义消息的去向。如果写成 withClue() {; },如果写成 {; ,则 message 会被添加到前面。 withClue(),附加消息
  2. 为了使用withClue作为中缀运算符,必​​须混合org.scalatest.AppishedClues,或者导入org.scalatest.AppishedClues.convertToClueful
  3. 如果未使用 infix 关键字定义,Scala 3 不允许将名称完全按字母顺序排列的方法用作中缀。为了解决这个问题,请将 withClue 括在反引号中,如下所示: {; } `withClue` ()

Few things that none of the existing answers mention.

  1. The position of withClue determines where the custom message goes. If written as withClue(<message>) { <assertion> }, the message is prepended, if written as { <assertion> } withClue(<message>), the message is appended.
  2. In order to use withClue as an infix operator, org.scalatest.AppendedClues must be mixed in, or org.scalatest.AppendedClues.convertToClueful imported.
  3. Scala 3 doesn't allow methods with entirely alphabetic names to be used as infix if not defined with the infix keyword. In order to get around this, enclose withClue in backticks, like so: { <assertion> } `withClue` (<message>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文