如何减少specs2中的故障显示

时间:2013-02-13 01:41:27

标签: unit-testing scala testing specs2

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

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

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

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

1 个答案:

答案 0 :(得分:2)

您可以通过实施自己的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