Play test-only不会忽略测试

时间:2014-04-18 18:58:30

标签: scala playframework

我有一个播放应用程序,我需要在编译和构建时忽略我的功能测试,然后才运行集成测试。

这是我的测试代码:

ApplicationSpec.scala

@RunWith(classOf[JUnitRunner])
class ApplicationSpec extends Specification {

   "Application" should {
      "send 404 on a bad request" in new WithApplication {
         route(FakeRequest(GET, "/boum")) must beNone
   }

}

IntegrationSpec.scala

@RunWith(classOf[JUnitRunner])
class IntegrationSpec extends Specification {

    "Application" should {
       "work from within a browser" in {
           running(TestServer(9000), FIREFOX) { browser =>
               browser.goTo("http://localhost:9000/app")
               browser.pageSource must contain("Your new application is ready.")
           }
       }
    } section "integration"
}

文档告诉我,我可以从命令行使用类似的东西:

play "test-only -- exclude integration"

唯一的问题是,这实际上并不排除任何测试,我的集成测试会调用firefox并开始运行。我究竟做错了什么?如何排除集成测试,然后自己运行它们?

1 个答案:

答案 0 :(得分:1)

sbt' s Testing documentation描述了几种定义单独测试配置的方法。

例如,在这种情况下可以使用Additional test configurations with shared sources示例:

  

在这种方法中,源使用相同的类路径编译在一起并打包在一起。但是,根据配置运行不同的测试。

在使用play new创建的默认Play Framework 2.2.2应用程序中,添加一个名为project/Build.scala的文件,其中包含以下内容:

import sbt._
import Keys._

object B extends Build {
  lazy val root =
    Project("root", file("."))
      .configs(FunTest)
      .settings(inConfig(FunTest)(Defaults.testTasks) : _*)
      .settings(
        libraryDependencies += specs,
        testOptions in Test := Seq(Tests.Filter(unitFilter)),
        testOptions in FunTest := Seq(Tests.Filter(itFilter))
      )

  def itFilter(name: String): Boolean = (name endsWith "IntegrationSpec")
  def unitFilter(name: String): Boolean = (name endsWith "Spec") && !itFilter(name)

  lazy val FunTest = config("fun") extend(Test)
  lazy val specs = "org.specs2" %% "specs2" % "2.0" % "test"
}

运行标准单元测试:

$ sbt test

产生:

[info] Loading project definition from /home/fernando/work/scratch/so23160453/project
[info] Set current project to so23160453 (in build file:/home/fernando/work/scratch/so23160453/)
[info] ApplicationSpec
[info] Application should
[info] + send 404 on a bad request
[info] + render the index page
[info] Total for specification ApplicationSpec
[info] Finished in 974 ms
[info] 2 examples, 0 failure, 0 error
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 3 s, completed Apr 22, 2014 12:42:37 PM

要为添加的配置运行测试(此处为" fun"),请在配置名称前加上:

$ sbt fun:test

产生:

[info] Loading project definition from /home/fernando/work/scratch/so23160453/project
[info] Set current project to so23160453 (in build file:/home/fernando/work/scratch/so23160453/)
[info] IntegrationSpec
[info] Application should
[info] + work from within a browser
[info] Total for specification IntegrationSpec
[info] Finished in 0 ms
[info] 1 example, 0 failure, 0 error
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[success] Total time: 6 s, completed Apr 22, 2014 12:43:17 PM

您也可以只测试特定的类,如下所示:

$ sbt "fun:testOnly IntegrationSpec"

请参阅GitHub中的this example