为什么在使用“sbt it:test"时没有执行 Play/Scala 项目中的集成测试? [英] Why are integration tests in a Play/Scala project not executed when using "sbt it:test"?
问题描述
我有一个 Play Framework 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.
使用标准源层次结构:
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
中设置集成测试 - 在另一个问题 sbt 0.13.x 中 IntegrationTest 配置的最小设置是什么? -- 归结为 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
就完成了.
you should add configs
and you're done.
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 项目中的位置,简短的回答是在 shell 中执行 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
应该适用于 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屋!