SBT:将任务 "test" 的行为覆盖为 运行 仅来自默认 testSource 文件夹的测试,有多个测试源文件夹

SBT : Override behavior of task "test" to run only tests from default testSource folder, there are multiple test source folders

我们有具有标准目录结构的标准 SBT 项目。

test 文件夹包含所有 JUnit 测试。

我们引入了一个新文件夹 othertests 和文件夹 othertests 与文件夹 test

对等

othertest文件夹包含一些特殊的测试用例。文件夹 othertest 中的测试 类 名称可以很容易地与普通的 JUnit 测试区分开来。

下面是我的工作 build.sbt 配置。

我使用 javaSources in Test SBT 任务在测试源中添加了文件夹 othertest

我们正在使用 activator 来 运行 测试和做其他事情。

当我 运行 activator> test 我想要文件夹 test 中的所有测试只到 运行 我想 运行 分别从文件夹 othertests 进行测试。

问题。

  1. 如何覆盖测试任务的行为以从文件夹 othertests
  2. 中过滤掉测试
  3. 我是否应该为 运行 普通 junit 测试和其他 junit 测试分别创建共享模块。

下面是我的build.sbt配置

import java.io.PrintStream

import play.sbt.PlayJava
import play.twirl.sbt.Import._

name := "Service"

version := "5.1.0"

scalaVersion := "2.11.8"

routesGenerator := InjectedRoutesGenerator

lazy val ContractTest = config("contract") extend(Test)

def contractTestFilter(name: String): Boolean = name endsWith "ContractTest"

def ignoreContractTest(name: String): Boolean = !contractTestFilter(name)

lazy val root = (project in file("."))
  .enablePlugins(PlayJava)
  .configs(ContractTest)
    .settings(
      inConfig(ContractTest) (Defaults.testTasks),
      javaSource in Test := { (baseDirectory in Test) (_ / "contracttest") }.value,
      testOptions in ContractTest := Seq(Tests.Filter(contractTestFilter),Tests.Argument(TestFrameworks.JUnit, "-q", "-v", "-s")),
      testOptions in Test := Seq(Tests.Filter(ignoreContractTest),Tests.Argument(TestFrameworks.JUnit, "-q", "-v", "-s"))
    )

lazy val xyz = taskKey[Unit]("custom task to create loglayout jar file under lib folder")

xyz := {
  LogLayoutJar.build(scalaBinaryVersion.value, streams.value.log)
}

run := (run in Runtime).dependsOn(xyz).evaluated

javaOptions in Test ++= Seq("-Dconfig.resource=applic.tt.cnf")

libraryDependencies ++= Seq(
  json,
  javaWs,
  "org.mockito" % "mockito-all" % "1.10.19" % Test,
  "org.scalatestplus.play" %% "scalatestplus-play" % "1.5.1" % Test,
  "org.easytesting" % "fest-assert" % "1.4" % Test,
  "org.scalactic" %% "scalactic" % "2.2.0",
  "org.jmockit" % "jmockit" % "1.9" % Test,
  "com.portingle" % "slf4jtesting" % "1.0.0" % Test,
  "org.scalacheck" %% "scalacheck" % "1.12.6" % Test
)

resolvers ++= Seq(
  "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
)

parallelExecution in Test := runningTestInParallel

testGrouping in Test := groupByModule((externalDependencyClasspath in Test).value,
  (definedTests in Test).value, streams.value.log)

javacOptions ++= Seq(
  "-Xlint:unchecked",
  "-Xlint:deprecation"
)
scalacOptions ++= Seq(
  "-feature",
  "-language:implicitConversions",
  "-deprecation"
)

// Custom tasks //
val silenceSystemErr = taskKey[Unit]("Replaces System.err with a PrintStream to nowhere.")

silenceSystemErr := {
  System.setErr(new PrintStream(new DevNull))
  println("Messages System.err will not be printed.")
}

val restoreSystemErr = taskKey[Unit]("Restores the original System.err")

restoreSystemErr := {
  System.setErr(systemErr)
  println("Messages System.err will be printed.")
}

我们从 jenkins 运行 使用以下命令进行测试 -

bin/activator -Dsbt.log.noformat=true -Denvironment_name=test -DsuppressLogging=true clean silenceSystemErr jacoco:cover

谢谢 拉凯什

1.

你在自相矛盾。为什么要添加 javaSources in Test 如果您不希望它们在 运行 Test 命令时添加 运行?

您应该做的是创建一个 [附加测试配置|http://www.scala-sbt.org/0.13/docs/Testing.html#Additional+test+configurations] 扩展 Test 并且 运行 仅在您的 othertests 文件夹中进行测试。

2.

您可以创建另一个模块。我个人不喜欢这个想法,因为我必须根据它测试的内容来命名模块,而且我有 2 个独立的模块,实际上应该只有一个。

如果您的测试中有一些依赖项会减慢模块的整体构建时间,那么单独的测试模块可能是个好主意。例如,想象一个带有 Gatling 性能测试的 Java 项目。如果性能测试在同一个模块中,那么每当我 rebuild 它也会 rebuild 需要较慢的 scala 编译器的 gatling 测试。

有些人可以忍受这一点,我就是其中之一。我更喜欢在同一个项目中进行测试,并且在重建模块时可能会受到时间损失。这种情况很少发生,我会在需要时创建不同的测试配置。

选择单独模块进行测试的另一个原因是当您的测试依赖于多个模块并且您不希望在 src 代码的编译时依赖此模块。

一些 IDE 的 and/or 语言可能鼓励使用单独的模块进行测试,据我所知,C# 和 Visual Studio 就是这种情况(但我在这里可能是错的,不要相信我的话)。