为什么在使用"sbt it:test"时Play/Scala项目中的集成测试没有执行?

Mik*_*ike 12 scala sbt playframework-2.3

我有一个Play Framework 2.3项目,我想将单元测试和功能测试分开如下:

  1. 运行sbt test应运行单元测试并排除集成测试
  2. 运行sbt it:test应该只运行集成测试

Scala文档建议使用project/Build.scala,但我想使用build.sbt和的组合project/Build.scala,所以我的配置看起来像这样(我也尝试将所有配置放入Build.scala):

build.sbt

....

libraryDependencies ++= Seq(
  "com.typesafe.play" %% "play-json" % "2.2.3",
  "org.scalatest" %% "scalatest" % "2.1.5" % "it, test",
  "org.mockito" % "mockito-all" % "1.9.5" % "it, test"
)

def funTestFilter(name: String): Boolean = ((name endsWith "ItTest") || (name endsWith "IntegrationTest"))
def unitTestFilter(name: String): Boolean = ((name endsWith "Test") && !funTestFilter(name))

testOptions in IntegrationTest := Seq(Tests.Filter(funTestFilter))

testOptions in Test := Seq(Tests.Filter(unitTestFilter))
Run Code Online (Sandbox Code Playgroud)

项目/ Build.scala

import sbt._

object Build extends Build {

  lazy val root =
    Project("root", file("."))
      .configs( IntegrationTest )
      .settings( Defaults.itSettings : _* )

}
Run Code Online (Sandbox Code Playgroud)

在此配置下,运行sbt test会排除我的集成测试(以其结尾IntegrationTest),但运行时sbt it:test不会发现任何测试.

文章建议将文件放在特定路径中,但我不知道Play项目的等效路径是什么.

使用标准源层次结构:

src/it/scala for Scala sources
src/it/java for Java sources
src/it/resources for resources that should go on the integration test classpath
Run Code Online (Sandbox Code Playgroud)

Jac*_*ski 22

第一部分 - 关于在build.sbt- 中设置集成测试- 在另一个问题中描述了什么是sbt 0.13.x中IntegrationTest配置的最小设置?- 它归结为以下条目build.sbt:

Defaults.itSettings

lazy val root = project.in(file(".")).configs(IntegrationTest)
Run Code Online (Sandbox Code Playgroud)

由于Play项目定义如下:

lazy val root = (project in file(".")).enablePlugins(PlayScala)
Run Code Online (Sandbox Code Playgroud)

你应该添加configs,你已经完成了.

lazy val root = project in file(".") enablePlugins(PlayScala) configs(IntegrationTest) 
Run Code Online (Sandbox Code Playgroud)

执行reload或重新启动sbt/activator会话并执行it:test.

[it-play] $ it:test
[info] Updating {file:/Users/jacek/sandbox/it-play/}root...
[info] Resolving jline#jline;2.11 ...
[info] Done updating.
[success] Total time: 1 s, completed Sep 13, 2014 7:12:27 PM
Run Code Online (Sandbox Code Playgroud)

至于在集成测试应该在播放项目去的地方,简单的答案是这样做show it:sourceDirectory的外壳,即src/itscala,java以及resources目录下包含相应的来源.

it配置中添加测试框架库,即build.sbt应该有类似的东西 - 注意it配置:

"org.specs2" %% "specs2" % "2.4.2" % "it"
Run Code Online (Sandbox Code Playgroud)

执行reloadsbt会话或重新启动它,并且it:test应该使用Specs2测试进行所有测试src/it/scala.

要缩小应该执行的测试,请添加过滤器 - 确保它在行之后,Defaults.itSettings因为顺序很重要,可以覆盖另一个:

val funTestFilter: String => Boolean = { name =>
  (name endsWith "ItTest") || (name endsWith "IntegrationTest")
}

testOptions in IntegrationTest += Tests.Filter(funTestFilter)
Run Code Online (Sandbox Code Playgroud)

执行reload或重新启动sbt并执行it:test.它应该工作.这样做show it:testOptions,以确保您设置的正确加载:

[it-play] $ show it:testOptions
[info] List(Filter(<function1>))
Run Code Online (Sandbox Code Playgroud)

build.sbt对我有用的全部内容如下 - 它是由以下内容创建的activator new it-play play-scala:

name := """it-play"""

version := "1.0-SNAPSHOT"

lazy val root = project in file(".") enablePlugins(PlayScala) configs(IntegrationTest)

scalaVersion := "2.11.1"

libraryDependencies ++= Seq(
  jdbc,
  anorm,
  cache,
  ws,
  "org.specs2" %% "specs2" % "2.4.2" % "it"
)

Defaults.itSettings

val funTestFilter: String => Boolean = { name =>
  (name endsWith "ItTest") || (name endsWith "IntegrationTest")
}

testOptions in IntegrationTest += Tests.Filter(funTestFilter)
Run Code Online (Sandbox Code Playgroud)

  • 令人困惑的是,Play的集成测试是"src/it"而不是"/ it",因为如果它与基本目录中的"/ app"和"/ test"并列,那将更有意义,因为[Play Anatomy] (https://www.playframework.com/documentation/2.4.x/Anatomy).我通过将`build.sbt`中的设置更改为在IntegrationTest中读取`sourceDirectory:= baseDirectory.value /"it"`来移动我的集成测试.然后它找到通常的`it/java`,`it/scala`,`it/resources`目录. (3认同)

Mik*_*ike 13

这就是我最终做的事情,这使我能够:

  1. 将我的集成测试保存在play的/test目录中
  2. 运行sbt test单元测试和sbt fun:test功能/集成测试

这是build.sbt,没有必要project/Build.scala

libraryDependencies ++= Seq(
  ...
  "com.typesafe.play" %% "play-json" % "2.2.3",
  "org.scalatest" %% "scalatest" % "2.1.5" % "test",
  "org.mockito" % "mockito-all" % "1.9.5" % "test"
)

lazy val FunTest = config("fun") extend(Test)

def funTestFilter(name: String): Boolean = ((name endsWith "ItTest") || (name endsWith "IntegrationTest"))
def unitTestFilter(name: String): Boolean = ((name endsWith "Test") && !funTestFilter(name))

lazy val root = project in file(".") configs(FunTest) settings( inConfig(FunTest)(Defaults.testTasks) : _*)

testOptions in FunTest := Seq(Tests.Filter(funTestFilter))

testOptions in Test := Seq(Tests.Filter(unitTestFilter))
Run Code Online (Sandbox Code Playgroud)