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

后端 未结 2 442
被撕碎了的回忆
被撕碎了的回忆 2021-02-02 16:39

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
相关标签:
2条回答
  • 2021-02-02 17:04

    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)
    
    0 讨论(0)
  • 2021-02-02 17:04

    This is what I ended up doing, which allows me to:

    1. keep my integration tests in play's /test directory
    2. run sbt test for Unit Tests and sbt fun:test for functional/integration tests

    Here is the build.sbt, no need for a 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))
    
    0 讨论(0)
提交回复
热议问题