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

时间:2014-09-12 23:08:48

标签: scala sbt playframework-2.3

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

  1. 运行sbt test应运行单元测试并排除集成测试
  2. 运行sbt it:test应仅运行集成测试
  3. Scala文档建议使用project/Build.scala,但我想使用build.sbtproject/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
    

2 个答案:

答案 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/itscalajava和下面的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)

这就是我最终要做的事情,这让我可以:

  1. 将我的集成测试保存在play的/test目录
  2. 运行sbt test进行单元测试,sbt fun:test运行功能/集成测试
  3. 以下是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))