使用"sbt it:test"时,为什么不执行Play/Scala项目中的集成测试? [英] Why are integration tests in a Play/Scala project not executed when using "sbt it:test"?
问题描述
我有一个 Play框架 2.3项目,我想将单元测试和功能测试分开如下:
I have a Play Framework 2.3 project in which I'd like to separate unit tests and functional tests as follows:
- 运行
sbt test
应该运行单元测试,并排除集成测试 - 运行
sbt it:test
应该仅运行集成测试
- running
sbt test
should run unit tests and exclude integration tests - running
sbt it:test
should run integration tests only
Scala文档建议使用project/Build.scala
,但是我想使用build.sbt
和project/Build.scala
的组合,所以我的配置看起来像这样(我也尝试将所有配置都放入Build.scala
):
The Scala documentation suggests using project/Build.scala
, but I'd like to use combination of build.sbt
and project/Build.scala
, so my configuration looks like this (I have also tried putting all of configuration into 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))
project/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
不会找到任何测试.
Under this configuration running sbt test
does exclude my integration test (which ends with IntegrationTest
) but running sbt it:test
finds no tests.
文章建议将文件放置在特定路径中,但我不知道Play项目的等效路径是什么.
The article suggests putting files in a specific path, but I don't know what the equivalent path is for the Play project.
使用标准的源层次结构:
The standard source hierarchy is used:
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
推荐答案
第一部分-关于在build.sbt
中设置集成测试-在另一个问题
The first part - about setting up integration tests in build.sbt
- is described in another question What would be the minimal setup of IntegrationTest configuration in sbt 0.13.x? -- it boils down to the following entries in build.sbt
:
Defaults.itSettings
lazy val root = project.in(file(".")).configs(IntegrationTest)
由于Play项目的定义如下:
Since a Play project is defined as follows:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
您应该添加configs
并完成.
lazy val root = project in file(".") enablePlugins(PlayScala) configs(IntegrationTest)
执行reload
或重新启动sbt/activator会话并执行it:test
.
Do reload
or restart the sbt/activator session and execute 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项目中进行的位置,简短的答案是在外壳中执行show it:sourceDirectory
,即src/it
并在其下的scala
,java
和resources
目录中各自的来源.
As to the place where the integration tests should go in a Play project, the short answer is do show it:sourceDirectory
in the shell, i.e. src/it
with scala
, java
and resources
directories beneath to contain respective sources.
在it
配置中添加测试框架库,即在build.sbt
中应该类似于以下内容-注意it
配置:
Add test framework library in it
configuration, i.e. in build.sbt
there should be something similar to - note the it
configuration:
"org.specs2" %% "specs2" % "2.4.2" % "it"
执行reload
sbt会话或重新启动它,并且it:test
应该与Specs2测试一起用于src/it/scala
下的所有测试.
Do reload
the sbt session or restart it, and it:test
should work with Specs2 tests for all tests under src/it/scala
.
要缩小应执行的测试范围,请添加过滤器-确保顺序在Defaults.itSettings
行之后,因为顺序确实很重要,并且一个可以覆盖另一个:
To narrow what tests should be executed, add your filter - make sure it's after the line with Defaults.itSettings
as the order does matter and one can override the other:
val funTestFilter: String => Boolean = { name =>
(name endsWith "ItTest") || (name endsWith "IntegrationTest")
}
testOptions in IntegrationTest += Tests.Filter(funTestFilter)
执行reload
或重新启动sbt并执行it:test
.它应该工作.执行show it:testOptions
以确保正确加载了设置:
Do reload
or restart sbt and execute it:test
. It should work. Do show it:testOptions
to ensure your setup's loaded properly:
[it-play] $ show it:testOptions
[info] List(Filter(<function1>))
为我工作的整个build.sbt
如下-由activator new it-play play-scala
创建:
The entire build.sbt
that worked for me is as follows - it's created by 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)
这篇关于使用"sbt it:test"时,为什么不执行Play/Scala项目中的集成测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!