如何在scalatest检测到故障时忽略测试实用程序方法?

fom*_*mil 6 scala scalatest

我在测试中有这种方便的方法:

  def assertFormat[T: SexpFormat](start: T, expect: Sexp): Unit = {
    val sexp = start.toSexp
    assert(sexp === expect, s"${sexp.compactPrint} was not ${expect.compactPrint}")
    expect.convertTo[T] should be(start)
  }
Run Code Online (Sandbox Code Playgroud)

这基本上是一个运行断言模式的便利,我做了很多.

Matcher由于隐含的要求,SexpFormat[T]我不可能将其重写为(虽然我有兴趣听到不需要我写入类型MyFormat的方法foo should roundTrip[MyFormat](...))

如果在此实用程序方法中任何测试失败,则scalatest会将内部标记assertFormat为导致测试失败的原因.但我真的希望scalatest能够检测到这种方法的调用者是测试的原因.我怎样才能做到这一点?

即电流输出是

[info] - should support custom missing value rules *** FAILED ***
[info]   SexpNil did not equal SexpCons(SexpSymbol(:duck),SexpCons(SexpNil,SexpNil)) nil was not (:duck nil) (FormatSpec.scala:11)
[info]   org.scalatest.exceptions.TestFailedException:
[info]   at org.scalatest.Assertions$class.newAssertionFailedException(Assertions.scala:529)
[info]   at org.scalatest.FlatSpec.newAssertionFailedException(FlatSpec.scala:1691)
[info]   at org.scalatest.Assertions$AssertionsHelper.macroAssert(Assertions.scala:502)
[info]   at org.ensime.sexp.formats.FormatSpec$class.assertFormat(FormatSpec.scala:11)
[info]   at org.ensime.sexp.formats.test.FamilyFormatsSpec.assertFormat(FamilyFormatsSpec.scala:151)
[info]   at org.ensime.sexp.formats.test.FamilyFormatsSpec.roundtrip(FamilyFormatsSpec.scala:156)
[info]   at org.ensime.sexp.formats.test.FamilyFormatsSpec$$anonfun$12.apply(FamilyFormatsSpec.scala:222)
[info]   at org.ensime.sexp.formats.test.FamilyFormatsSpec$$anonfun$12.apply(FamilyFormatsSpec.scala:221)
Run Code Online (Sandbox Code Playgroud)

FormatSpec.scala:11是我assertFormat的定义.真正的失败在于FamilyFormatsSpec.scala:222(这称为另一种便利方法FamilyFormatsSpec.scala:156)

Bil*_*ers 2

org.scalactic.source.Position这在 ScalaTest 3.0 中可以通过在自定义断言中隐含实现。然后,每当调用您的方法时,都会计算该位置(通过宏)assertFormat,并且该位置将由 中的断言和匹配器表达式选取assertFormat。其外观如下:

import org.scalactic.source

def assertFormat[T: SexpFormat](start: T, expect: Sexp)(implicit pos: source.Position): Unit = {
  val sexp = start.toSexp
  assert(sexp === expect, s"${sexp.compactPrint} was not ${expect.compactPrint}")
  expect.convertTo[T] should be(start)
}
Run Code Online (Sandbox Code Playgroud)

下面的例子说明了这一点。如果类路径上有 ScalaTest 3.0,只需 :load 以下文件到 Scala REPL 中:

:paste

import org.scalatest._
import org.scalactic._
import Matchers._

case class Sexp(o: Any) {
  def compactPrint: String = o.toString
  def convertTo[T: SexpFormat]: Sexp = implicitly[SexpFormat[T]].convertIt(o)
  override def toString = "I'm too sexp for my shirt."
}

trait SexpFormat[T] {
  def convertIt(o: Any): Sexp = new Sexp(o)
}

implicit class Sexpify(o: Any) {
  def toSexp: Sexp = new Sexp(o)
}

implicit def universalSexpFormat[T]: SexpFormat[T] = new SexpFormat[T] {}

def assertFormat[T: SexpFormat](start: T, expect: Sexp): Unit = {
  val sexp = start.toSexp
  assert(sexp === expect, s"${sexp.compactPrint} was not ${expect.compactPrint}")
  expect.convertTo[T] should be(start)
}

import org.scalatest.exceptions.TestFailedException

val before = intercept[TestFailedException] { assertFormat(1, new Sexp) }

println(s"${before.failedCodeStackDepth} - This stack depth points to the assert call inside assertFormat")

import org.scalactic.source

def betterAssertFormat[T: SexpFormat](start: T, expect: Sexp)(implicit pos: source.Position): Unit = {
  val sexp = start.toSexp
  assert(sexp === expect, s"${sexp.compactPrint} was not ${expect.compactPrint}")
  expect.convertTo[T] should be(start)
}

val after = intercept[TestFailedException] { betterAssertFormat(1, new Sexp) }

println(s"${after.failedCodeStackDepth} - This stack depth is the betterAssertFormat call itself in your test code")
Run Code Online (Sandbox Code Playgroud)

它将打印:

3 - This stack depth points to the assert call inside assertFormat
4 - This stack depth is the betterAssertFormat call itself in your test code
Run Code Online (Sandbox Code Playgroud)