Play框架中的集成测试

nop*_*lay 4 testing integration scala sbt playframework

我试图让集成测试在Play Framework 2.1.1中运行

我的目标是能够在单元测试后运行集成测试,以测试底层数据库的组件级功能.底层数据库将具有存储过程,因此我必须做的不仅仅是您可以在伪应用程序中配置的"inMemoryDatabase".

我希望这个过程是:

  1. 使用FakeApplication启动TestServer.使用备用集成测试配置文件
  2. 运行集成测试(可以按包过滤,也可以不在此处过滤)
  3. 停止TestServer

我相信最好的方法是在Build.scala文件中.

我需要有关如何设置Build.scala文件的帮助,以及如何加载替代集成测试配置文件(project/it.conf)

非常感谢任何帮助!

nop*_*lay 6

我已经介绍了一种暂时可行的方法.我想看看Play在sbt中引入了单独的"Test"与"IntegrationTest"范围的概念.

我可以进入Play,看看他们如何在sbt中构建他们的项目和设置,并尝试使IntegrationTest范围工作.现在,我花了太多时间试图让它运作起来.

我所做的是创建一个Specs Around Scope类,使我能够强制执行TestServer的单例实例.使用该类的任何内容都将尝试启动测试服务器,如果它已在运行,则不会重新启动它.

似乎Play和SBT在确保服务器在测试结束时关闭时做得很好,这到目前为止是有效的.

这是示例代码.仍希望得到更多反馈.

class WithTestServer(val app: FakeApplication = FakeApplication(),
        val port: Int = Helpers.testServerPort) extends Around with Scope {
      implicit def implicitApp = app
      implicit def implicitPort: Port = port

      synchronized {
        if ( !WithTestServer.isRunning ) {
          WithTestServer.start(app, port)
        }
      }

      // Implements around an example
      override def around[T: AsResult](t: => T): org.specs2.execute.Result = {
        println("Running test with test server===================")
        AsResult(t)
      }
    }

    object WithTestServer {

      var singletonTestServer: TestServer = null

      var isRunning = false

      def start(app: FakeApplication = FakeApplication(), port: Int = Helpers.testServerPort) = {
          implicit def implicitApp = app
          implicit def implicitPort: Port = port
          singletonTestServer = TestServer(port, app)
          singletonTestServer.start()
          isRunning = true
      }

    }
Run Code Online (Sandbox Code Playgroud)

为了更进一步,我只需在play/test文件夹中设置两个文件夹(包): - test/unit(test.unit包) - test/integration(test.integration pacakage)

现在,当我从Jenkins服务器运行时,我可以运行:

play test-only test.unit.*Spec

这将执行所有单元测试.

要运行我的集成测试,我运行:

play test-only test.integration.*Spec

而已.这对我来说暂时适用,直到Play添加集成测试作为生命周期步骤.

  • 很好的答案,唯一的问题是包命名 - "test.integration",`test.unit`会在测试方面使`private [service]`无效; 相反,你只需重组以分离`test`,`integration`文件夹并在构建文件中配置它们. (3认同)
  • 我还添加了别名来运行那些简单的:`addCommandAlias("unit","testOnly unit.*Spec")``addCommandAlias("integration","testOnly integration.*Spec")`然后:`play unit `和`播放整合' (2认同)