Mik*_*ike 12 scala sbt playframework-2.3
我有一个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
Jac*_*ski 22
第一部分 - 关于在build.sbt- 中设置集成测试- 在另一个问题中描述了什么是sbt 0.13.x中IntegrationTest配置的最小设置?- 它归结为以下条目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
至于在集成测试应该在播放项目去的地方,简单的答案是这样做show it:sourceDirectory的外壳,即src/it用scala,java以及resources目录下包含相应的来源.
在it配置中添加测试框架库,即build.sbt应该有类似的东西 - 注意it配置:
"org.specs2" %% "specs2" % "2.4.2" % "it"
执行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)
执行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)
Mik*_*ike 13
这就是我最终做的事情,这使我能够:
/test目录中sbt test单元测试和sbt fun:test功能/集成测试这是build.sbt,没有必要project/Build.scala 
Run Code Online (Sandbox Code Playgroud)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))
| 归档时间: | 
 | 
| 查看次数: | 7454 次 | 
| 最近记录: |