4

我的目标是将 jacoco 集成到我的 play 2.2.0 项目中。我尝试遵循的网络上的不同指南大多增加了混乱,而不是接近目标。

更添混乱

  • 大多数指南假定存在 build.sbt
    • 它似乎被具有不同的 build.scala 所取代
  • 有一个 jacoco4sbt 和一个普通的 jacoco
    • 哪一个最适合与 scala play framework 2 一起使用

当前状态

在 plugins.sbt 添加

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

在 build.scala 添加

import de.johoop.jacoco4sbt.JacocoPlugin._
lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)

通过这些更改,我在 sbt 和游戏控制台中都没有得到“jacoco”任务。

什么是让这个工作的适当步骤?

根据要求更新 build.scala 的内容

import com.typesafe.sbt.SbtNativePackager._
import com.typesafe.sbt.SbtScalariform._
import play.Project._
import sbt.Keys._
import sbt._
import sbtbuildinfo.Plugin._
import de.johoop.jacoco4sbt.JacocoPlugin._

object BuildSettings {
  val buildOrganization = "XXXXX"
  val buildVersion      = "0.1"
  val buildScalaVersion = "2.10.2"
  val envConfig = "-Dsbt.log.format=false -Dconfig.file=" + Option(System.getProperty("env.config")).getOrElse("local.application")
  scalacOptions ++= Seq("-encoding", "UTF-8", "-deprecation", "-unchecked", "-feature")
  javaOptions += envConfig

  val buildSettings = Defaults.defaultSettings ++ Seq (
    organization := buildOrganization,
    version      := buildVersion,
    scalaVersion := buildScalaVersion
  )
}

object Resolvers {
  val remoteRepoUrl =  "XXXXXXXXXXXX" at "http://nexus.cXXXXX/content/repositories/snapshots/"
  val publishRepoUrl = "XXXXXXXXXXXX" at "http://nexus.ciXXXXXXXXXXXXXXXX/content/repositories/snapshots/"
}

object Dependencies {
  val ods =  "XXXXXXXXX" % "XXXXXX-ws" % "2.2.1-SNAPSHOT"
  val scalatest = "org.scalatest" %% "scalatest" % "2.0.M8" % "test"
  val mockito = "org.mockito" % "mockito-all" % "1.9.5" % "test"
}

object ApplicationBuild extends Build {

  import BuildSettings._
  import Dependencies._
  import Resolvers._

  // Sub-project specific dependencies
  val commonDeps = Seq(
    ods,
    scalatest,
    mockito
  )
  //val bN = settingKey[Int]("current build Number")
  val gitHeadCommitSha = settingKey[String]("current git commit SHA")
  val release = settingKey[Boolean]("Release")

  lazy val jacoco_settings = Defaults.defaultSettings ++ Seq(jacoco.settings: _*)

  lazy val nemo = play.Project(
    "nemo",
    path = file("."),
    settings = Defaults.defaultSettings ++ buildSettings ++
      Seq(libraryDependencies ++= commonDeps) ++
      Seq(scalariformSettings: _*) ++
      Seq(playScalaSettings: _*) ++
      buildInfoSettings ++
      jacoco_settings ++
      Seq(
        sourceGenerators in Compile <+= buildInfo,
        buildInfoKeys ++= Seq[BuildInfoKey](
          resolvers,
          libraryDependencies in Test,
          buildInfoBuildNumber,
          BuildInfoKey.map(name) { case (k, v) => "project" + k.capitalize -> v.capitalize },
          "envConfig" -> envConfig, // computed at project load time
          BuildInfoKey.action("buildTime") {
            System.currentTimeMillis
          } // re-computed each time at compile
        ),
        buildInfoPackage := "com.springer.nemo"
      ) ++
      Seq(resolvers += remoteRepoUrl) ++
      Seq(mappings in Universal ++= Seq(
        file("ops/rpm/start-server.sh") -> "start-server.sh",
        file("ops/rpm/stop-server.sh") -> "stop-server.sh"
      ))
  ).settings(version <<=  version in ThisBuild)

  lazy val nemoPackaging = Project(
    "nemoPackaging",
    file("nemoPackaging"),
    settings = Defaults.defaultSettings ++Seq(Packaging.settings:_*)
  )



  def publishSettings =
    Seq(
      publishTo := Option(publishRepoUrl),
      credentials += Credentials(
        "Repo", "http://mycompany.com/repo", "admin", "admin123"))

}

注意:jacoco 正在使用它运行,但不接受我们的测试。输出:

jacoco:cover
[info] Compiling 1 Scala source to /home/schl14/work/nemo/target/scala-2.10/classes...
[info] ScalaTest
[info] Run completed in 13 milliseconds.
[info] Total number of tests run: 0
[info] Suites: completed 0, aborted 0
[info] Tests: succeeded 0, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for nemo/jacoco:test
4

3 回答 3

4

我通过这样做解决了它。

将以下内容添加到 plugins.sbt

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

在 build.scala 我添加了一个新的导入

import de.johoop.jacoco4sbt.JacocoPlugin._

并像这样将 jacoco 添加到配置部分

lazy val xyz = play.Project(
    "xyz",
    path = file("."),
    settings = Defaults.defaultSettings
      jacoco.settings ++ //this is the important part. 
  ).settings(parallelExecution in jacoco.Config := false) //not mandatory but needed in `most cases as most test can not be run in parallel`

在这些步骤之后,jacoco:cover 在 sbt 和 play 控制台中可用,并且还发现了我们的测试。

于 2013-11-07T16:08:52.410 回答
3

Scala 代码覆盖率的更好选择是 Scoverage,它提供语句行覆盖率。 https://github.com/scoverage/scalac-scoverage-plugin

添加到项目/plugins.sbt:

addSbtPlugin("com.sksamuel.scoverage" % "sbt-scoverage" % "1.0.1")

然后运行 ​​SBT

sbt clean coverage test
于 2014-01-12T21:02:37.103 回答
1

jacoco4sbt是衡量 Java 代码覆盖率的最佳选择。

添加到项目/plugins.sbt:

addSbtPlugin("de.johoop" % "jacoco4sbt" % "2.1.2")

在 build.sbt 末尾添加:

jacoco.settings

然后在终端运行:

//or just the sbt command and then use your browser
sbt jacoco:cover && /usr/bin/x-www-browser target/scala-2.10/jacoco/html/index.html

Scala 代码覆盖率也可以由SCCT确定。

添加到项目/plugins.sbt:

addSbtPlugin("com.github.scct" % "sbt-scct" % "0.2.1")

在 build.sbt 末尾添加:

ScctPlugin.instrumentSettings

然后查看覆盖范围:

sbt scct:test && /usr/bin/x-www-browser target/scala_2.10/coverage-report/index.html

也许你得到错误

请使用 --allow-file-access-from-files 重新启动浏览器或使用其他浏览器。

也许您使用 chrome 并且安全设置禁止对本地文件进行动态操作。您可以使用 firefox 打开页面或使用python -m SimpleHTTPServer 8000将其绑定到 http protokoll 并打开http://localhost:8000/target/scala-2.10/coverage-report/.

您可以在邮件列表中找到哪些生成的类应从报告中排除的灵感。

我的测试项目在GitHub 上

于 2013-11-05T18:48:17.850 回答