4

当 SBT 中的 ScalaTest 测试失败时,我会得到堆栈跟踪。我尝试set traceLevel in Test := -1过 SBT 提示,尝试更改build.sbt文件中的内容等,但似乎没有任何帮助。

我想摆脱的是:

> test
[info] TestWritingFunctions:
[info] - threeSquares examples *** FAILED ***
[info]   scala.NotImplementedError: an implementation is missing
[info]   at scala.Predef$.$qmark$qmark$qmark(Predef.scala:252)
[info]   at TestWritingFunctions$$anonfun$1.apply$mcV$sp(WritingFunctions.scala:17)
[info]   at TestWritingFunctions$$anonfun$1.apply(WritingFunctions.scala:16)
[info]   at TestWritingFunctions$$anonfun$1.apply(WritingFunctions.scala:16)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer$$anonfun$apply$1.apply(Transformer.scala:22)
[info]   at org.scalatest.OutcomeOf$class.outcomeOf(OutcomeOf.scala:85)
[info]   at org.scalatest.OutcomeOf$.outcomeOf(OutcomeOf.scala:104)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:22)
[info]   at org.scalatest.Transformer.apply(Transformer.scala:20)
[info]   ...

正如您所看到的,测试失败了,因为我使用了新的???构造作为占位符,但是由于我知道那里有一个占位符,所以我想要*** FAILED ***一条没有所有 rigamarole 的消息。那可能吗?

4

2 回答 2

4

You can use "-o..." without F or S (i.e, "-o" or "-oD" would give you no stack traces, but not "-oF", "-oS", or "-oDS" would give you a stack trace). That means you'd be suppressing all stack traces. If you specify no reporters at all, you'll get a "-o", which means no stack traces.

If you like the short stack traces in general, but don't want to see them when a NotImplementedError is thrown by ???, you can override withFixture and change NotImplementedError into pending tests:

import org.scalatest._

trait PendingIfUnimplemented extends SuiteMixin { this: Suite =>

  abstract override def withFixture(test: NoArgTest): Outcome = {
    super.withFixture(test) match {
      case Failed(ex: NotImplementedError) => Pending
      case other => other 
    }
  }
}

This way you'll still get short, long, or no stack traces for regular failures, whatever you chose, but see (pending) for tests that fail because of ???.

于 2013-10-18T05:39:59.913 回答
2

(不权威,但由于没有其他人回答:)

我怀疑这traceLevel没有做任何事情,因为堆栈跟踪来自 ScalaTest,而不是来自 sbt。

sbt 确实有一个logLevel相关的设置。尝试这个:

set logLevel in Test := Level.Warn

但请注意,这不仅会抑制堆栈跟踪,还会抑制示例中以 . 开头的所有行[info]。相反,您只会[error]在最后得到这些东西,其中至少包含测试失败的套件的名称。

如果您真的只想抑制堆栈跟踪而不更改任何其他内容,我认为这可能是不可能的。查看http://www.scalatest.org/user_guide/using_the_runner,我看到了使堆栈跟踪更短(-oS)或更长(-oF)的选项,但没有完全省略堆栈跟踪的选项。

于 2013-10-17T15:40:00.660 回答