scala 使用 sbt 和 testng 时,如何获取测试中抛出的异常的完整堆栈跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7237824/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 03:24:45  来源:igfitidea点击:

How can I get complete stacktraces for exceptions thrown in tests when using sbt and testng?

scalatestngsbtscalatestslf4j

提问by wn-

The stacktraces are truncated - e.g. they end with [info] ...

堆栈跟踪被截断 - 例如,它们以 [info] ...

Using lastor changing traceLeveldoesn't help - it simply prints the complete stacktrace of the sbt wrapper.

使用last或更改traceLevel无济于事 - 它只是打印 sbt 包装器的完整堆栈跟踪。

This is testing with testng (also I believe using scalatest and sl4j)

这是使用 testng 进行测试(我也相信使用 scalatest 和 sl4j)

回答by David

Using hints found in the documentation here:

使用此处文档中的提示:

(quoted)

(引)

You can configure the output shown when running with sbt in four ways: 1) turn off color, 2) show short stack traces, 3) full stack traces, and 4) show durations for everything. To do so you must pass a -o argument to ScalaTest, and after the -o, place any combination of:

  • D - show durations
  • S - show short stack traces
  • F - show full stack traces
  • W - without color

For example, "-oDF" would show full stack traces and durations (the amount of time spent in each test).

To pass arguments from sbt to ScalaTest you can either add test options globally, like this:

testOptions in Test += Tests.Argument("-oD")

您可以通过四种方式配置使用 sbt 运行时显示的输出:1) 关闭颜色,2) 显示短堆栈跟踪,3) 完整堆栈跟踪,以及 4) 显示所有内容的持续时间。为此,您必须将 -o 参数传递给 ScalaTest,并在 -o 之后放置以下任意组合:

  • D - 显示持续时间
  • S - 显示短堆栈跟踪
  • F - 显示完整的堆栈跟踪
  • W - 无色

例如,“-oDF”将显示完整的堆栈跟踪和持续时间(在每个测试中花费的时间)。

要将参数从 sbt 传递给 ScalaTest,您可以全局添加测试选项,如下所示:

testOptions in Test += Tests.Argument("-oD")

(See the website for the rest of the quote)

(有关报价的其余部分,请参阅网站)

You can use the following sbt command to enable full stack traces in tests:

您可以使用以下 sbt 命令在测试中启用完整堆栈跟踪:

> set testOptions in YourProjectName += Tests.Argument("-oF")


Per Sasha's comment, this can also be done from the command line per test run as shown below.

根据 Sasha 的评论,这也可以在每次测试运行时从命令行完成,如下所示。

$ sbt test -- -oF

回答by Kipton Barros

As an alternative to getting SBT to print the full stack trace, could you put a try-catchblock around your test runner? For example, from the REPL:

作为让 SBT 打印完整堆栈跟踪的替代方法,您能否在测试运行程序周围放置一个try-catch块?例如,来自 REPL:

scala> try { throw new Exception } catch { case e => e }
res1: java.lang.Throwable = java.lang.Exception

scala> res1.printStackTrace
java.lang.Exception
    at $line2.$read$$iw$$iw$.liftedTree1(<console>:8)
    at $line2.$read$$iw$$iw$.<init>(<console>:8)
    at $line2.$read$$iw$$iw$.<clinit>(<console>)
    ...