Search code examples
scalasbtplayframework-2.3

Why are integration tests in a Play/Scala project not executed when using "sbt it:test"?


I have a Play Framework 2.3 project in which I'd like to separate unit tests and functional tests as follows:

  1. running sbt test should run unit tests and exclude integration tests
  2. running sbt it:test should run integration tests only

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 : _* )

}

Under this configuration running sbt test does exclude my integration test (which ends with IntegrationTest) but running sbt it:test finds no tests.

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

Solution

  • 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)
    

    Since a Play project is defined as follows:

    lazy val root = (project in file(".")).enablePlugins(PlayScala)
    

    you should add configs and you're done.

    lazy val root = project in file(".") enablePlugins(PlayScala) configs(IntegrationTest) 
    

    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
    

    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.

    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"
    

    Do reload the sbt session or restart it, and it:test should work with Specs2 tests for all tests under src/it/scala.

    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)
    

    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>))
    

    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)