【问题标题】:Generate html report with ScalaTest 3.0使用 ScalaTest 3.0 生成 html 报告
【发布时间】:2020-03-22 08:35:44
【问题描述】:

This question 询问如何使用 sbt 和 ScalaTest 获取 html 报告。答案参考了 Scala 测试 2.0,似乎不适用于 ScalaTest 3.0

我通过

声明ScalaTest
    lazy val scalaTest = Seq("org.scalatest" %% "scalatest" % "3.0.8" % "test",
                             "org.scalactic" %% "scalactic" % "3.0.8",
                             "org.scalamock" %% "scalamock" % "4.4.0" % Test)

然后使用它

 ThisBuild / Test / testOptions += Tests.Argument(TestFrameworks.ScalaTest, "-h", "target/test-reports")

lazy val foo = (project in file("foo")).
    settings(libraryDependencies ++= scalaTest)

这失败了

[error] java.lang.NoClassDefFoundError: org/pegdown/PegDownProcessor
[error]     at org.scalatest.tools.HtmlReporter.<init>(HtmlReporter.scala:117)
[error]     at org.scalatest.tools.ReporterFactory.createHtmlReporter(ReporterFactory.scala:192)
[error]     at org.scalatest.tools.ReporterFactory.getReporterFromConfiguration(ReporterFactory.scala:239)
[error]     at org.scalatest.tools.ReporterFactory.$anonfun$createReportersFromConfigurations$1(ReporterFactory.scala:248)
[error]     at scala.collection.TraversableLike.$anonfun$map$1(TraversableLike.scala:237)
[error]     at scala.collection.Iterator.foreach(Iterator.scala:941)
[error]     at scala.collection.Iterator.foreach$(Iterator.scala:941)
[error]     at scala.collection.AbstractIterator.foreach(Iterator.scala:1429)
[error]     at scala.collection.IterableLike.foreach(IterableLike.scala:74)
[error]     at scala.collection.IterableLike.foreach$(IterableLike.scala:73)
[error]     at org.scalatest.tools.ReporterConfigurations.foreach(ReporterConfiguration.scala:42)
...

问题建议使用"test-&gt;*" 进行测试声明。尝试

    lazy val scalaTest = Seq("org.scalatest" %% "scalatest" % "3.0.8" % "test->*" excludeAll (
                                 ExclusionRule(organization="org.junit", name="junit")),
                             "org.scalamock" %% "scalamock" % "4.4.0" % Test
        )

反而失败了

[info] Compiling 1 Scala source to /projects/foo/target/scala-2.12/test-classes ...
[error] /projects/foo/src/test/scala/com/example/FooTest.scala:17:73: Symbol 'type org.scalactic.TripleEquals' is missing from the classpath.
[error] This symbol is required by 'trait org.scalatest.Assertions'.
[error] Make sure that type TripleEquals is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Assertions.class' was compiled against an incompatible version of org.scalactic.
[error] class InfoGainTest extends FlatSpec with Matchers with LoneElement with LazyLogging {
[error]                                                                         ^
[error] /projects/foo/src/test/scala/com/example/FooTest.scala:17:42: Symbol 'type org.scalactic.Tolerance' is missing from the classpath.
[error] This symbol is required by 'trait org.scalatest.Matchers'.
[error] Make sure that type Tolerance is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Matchers.class' was compiled against an incompatible version of org.scalactic.
[error] class InfoGainTest extends FlatSpec with Matchers with LoneElement with LazyLogging {
[error]                                          ^
[error] /projects/foo/src/test/scala/com/example/FooTest.scala:22:54: Symbol 'term org.scalactic.source' is missing from the classpath.
[error] This symbol is required by 'value org.scalatest.Matchers.pos'.
[error] Make sure that term source is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Matchers.class' was compiled against an incompatible version of org.scalactic.
[error]     def printGain(gainByProbe: Map[Probe, Double]) = logger.info("Info gain: {}",
[error]                                                      ^
[error] /projects/foo/src/test/scala/com/example/FooTest.scala:27:17: value should is not a member of String

随后出现的错误看起来像是不存在 FlatSpec 的隐式转换,并且可能是上述后续错误。

ScalaTest 3.0 有没有办法做到这一点?

【问题讨论】:

    标签: scala sbt scalatest


    【解决方案1】:

    尝试像这样添加pegdown依赖

    libraryDependencies += "org.pegdown" % "pegdown" % "1.6.0" % Test
    

    根据resolve #1201 replace pegdown with flexmark-java #1229,ScalaTest 3.1.x pegdown 中的注释被替换为 flexmark-java

    【讨论】:

    • 这行得通,尽管您也可以将% Test 添加到 pegdown 依赖项的末尾。
    猜你喜欢
    • 1970-01-01
    • 2013-09-14
    • 2016-08-31
    • 2012-11-13
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    相关资源
    最近更新 更多