我有一个Play Framework 2.3项目,我希望将单元测试和功能测试分开如下:
sbt test
应运行单元测试并排除集成测试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))
项目/ Build.scala
import sbt._
object Build extends Build {
lazy val root =
Project("root", file("."))
.configs( IntegrationTest )
.settings( Defaults.itSettings : _* )
}
在此配置下,运行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
答案 0 :(得分:22)
第一部分 - 关于在build.sbt
中设置集成测试 - 在另一个问题What would be the minimal setup of IntegrationTest configuration in sbt 0.13.x?中有所描述 - 它归结为build.sbt
中的以下条目:
Defaults.itSettings
lazy val root = project.in(file(".")).configs(IntegrationTest)
由于Play项目的定义如下:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
您应该添加configs
,然后就完成了。
lazy val root = project in file(".") enablePlugins(PlayScala) configs(IntegrationTest)
执行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
关于集成测试应该在Play项目中的位置,简短的答案是在shell中执行show it:sourceDirectory
,即src/it
与scala
,java
和下面的resources
目录包含各自的来源。
在it
配置中添加测试框架库,即在build.sbt
中应该有类似的内容 - 请注意it
配置:
"org.specs2" %% "specs2" % "2.4.2" % "it"
执行reload
sbt会话或重新启动它,it:test
适用于src/it/scala
下所有测试的Specs2测试。
要缩小应执行的测试,请添加过滤器 - 确保它位于Defaults.itSettings
行之后,因为顺序很重要,而且可以覆盖另一个:
val funTestFilter: String => Boolean = { name =>
(name endsWith "ItTest") || (name endsWith "IntegrationTest")
}
testOptions in IntegrationTest += Tests.Filter(funTestFilter)
执行reload
或重新启动sbt并执行it:test
。它应该工作。请show it:testOptions
确保您的设置正确加载:
[it-play] $ show it:testOptions
[info] List(Filter(<function1>))
对我有用的整个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)
答案 1 :(得分:13)
这就是我最终要做的事情,这让我可以:
/test
目录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))