unit-testing - 如何减少specs2中的故障显示

标签 unit-testing scala testing specs2

我正在尝试使用 specs2 比较两个非常大的数组。不幸的是,当数组不相等时,它会显示每个数组的实际内容和预期内容。无论如何,我是否可以减少实际和预期显示的数据量,或者仅针对此特定测试完全删除它。

我尝试过使用 setMessage 但这不会影响实际和预期部分。

bytes1 must be_== (bytes2).setMessage("A does not mach B")

我实际上想做的是比较两个输入流。我也有兴趣听听是否有人对如何做到这一点有更好的想法,而不是将它们转换为数组。

最佳答案

您可以通过实现自己的 Diffs 来控制差异的处理方式特质:

import org.specs2._
import main._

class MyDiffs extends Diffs {
  /** @return true if the differences must be shown */
  def show: Boolean = true
  /** @return true if the differences must be shown for 2 different strings */
  def show(expected: String, actual: String): Boolean = 
    expected.size + actual.size < 100
  /** @return the diffs */
  def showDiffs(expected: String, actual: String): (String, String) = 
    (expected.take(10).mkString, actual.take(10).mkString)
  /** @return true if the full strings must also be shown */
  def showFull: Boolean = false
  /** this method is not used and will be removed from the trait in a next release */
  def separators: String = ""  
}

class s extends Specification { def is = 
  args.report(diffs = new MyDiffs)^
  "test" ! {
    "abcdefghijklmnopqrstu" must_== "abcdefghijklmnopqrstuvwxyz"
  }
}


x test
'abcdefghijklmnopqrstu' is not equal to 'abcdefghijklmnopqrstuvwxyz' (<console>:47)
 Expected: qrstuvwxyz
 Actual:   lmnopqrstu

关于unit-testing - 如何减少specs2中的故障显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14845109/

相关文章:

javascript - 如何在 Jest 中重置模拟值?

ios - 依赖注入(inject)的公共(public)属性或自定义初始化程序?

scala - Play框架2.4国际化和i18n.Messages - Scala

scala - Scala中对象的Mockito

ios - 使用我日常使用的 iPhone 进行 iOS 应用程序设备测试

java - 我如何对以下锁和多线程代码进行单元测试

reactjs - 用jest和enzyme窥探React功能组件方法;无法监视原始值

algorithm - 我如何确定所有 Actor 都收到了广播消息

c# - 通信协议(protocol)的单元测试

php - 如何在 laravel 测试中获取 header 值