【问题标题】:Alternative of OneAppPerSuite in specs2 scala testing在 specs2 scala 测试中替代 OneAppPerSuite
【发布时间】:2016-10-19 21:31:30
【问题描述】:

我正在使用 specs2 编写单元测试用例,我的应用程序为每个测试实例启动和停止。

import org.specs2.mutable._

class HelloWorldSpec extends Specification {

  "The 'Hello world' string" should {
    "contain 11 characters" in new WithApplication {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in new WithApplication {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in new WithApplication {
      "Hello world" must endWith("world")
    }
  }
}

如文档中所述,每个测试用例应用程序都会启动和停止。

我从link 找到了解决方法。应用程序只为每个测试类初始化一次(我还没有测试过)。

import org.specs2.mutable._

class HelloWorldSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }
  step(Play.stop())
}

但是,如果我们有多个类并且我们想要应用程序的单一启动和停止。

import org.specs2.mutable._

class HelloWorldSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      "Hello world" must have size(11)
    }
    "start with 'Hello'" in {
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      "Hello world" must endWith("world")
    }
  }
  step(Play.stop())
}

import org.specs2.mutable._

class HitchHikerSpec extends Specification {sequential

  step(Play.start(App)) //supposedly App is iniatilized

  "The 'Hitch Hiker' string" should {
    "contain 11 characters" in {
      "Hitch Hiker" must have size(11)
    }
    "start with 'Hitch'" in {
      "Hitch Hiker" must startWith("Hitch")
    }
    "end with 'Hiker'" in {
      "Hitch Hiker" must endWith("Hiker")
    }
  }
  step(Play.stop())
}

如何启动和停止应用程序一次?

scalatest 中使用OneAppPerSuite 实现了类似的解决方案。 这是link 和示例。

import play.api.test._
import org.scalatest._
import org.scalatestplus.play._
import play.api.{Play, Application}
import play.api.inject.guice._

// This is the "master" suite
class NestedExampleSpec extends Suites(
  new OneSpec,
  new TwoSpec,
  new RedSpec,
  new BlueSpec
) with OneAppPerSuite {
  // Override app if you need an Application with other than non-default parameters.
  implicit override lazy val app: Application =
    new GuiceApplicationBuilder().configure(Map("ehcacheplugin" -> "disabled")).build()
}

// These are the nested suites
@DoNotDiscover class OneSpec extends PlaySpec with ConfiguredApp
@DoNotDiscover class TwoSpec extends PlaySpec with ConfiguredApp
@DoNotDiscover class RedSpec extends PlaySpec with ConfiguredApp

@DoNotDiscover
class BlueSpec extends PlaySpec with ConfiguredApp {

  "The OneAppPerSuite trait" must {
    "provide an Application" in {
      app.configuration.getString("ehcacheplugin") mustBe Some("disabled")
    }
    "make the Application available implicitly" in {
      def getConfig(key: String)(implicit app: Application) = app.configuration.getString(key)
      getConfig("ehcacheplugin") mustBe Some("disabled")
    }
    "start the Application" in {
      Play.maybeApplication mustBe Some(app)
    }
  }
}

可以在 specs2 中实现类似的东西吗?

【问题讨论】:

    标签: scala unit-testing testing playframework-2.0 specs2


    【解决方案1】:

    使用 specs2,您可以使用 specification references 做类似的事情:

    class SuiteSpec extends Specification { def is = s2"""
      ${link(StartSpec).hide}
      ${ "first spec"  ~ new Spec1Spec }
      ${ "second spec" ~ new Spec2Spec }
      ${link(StopSpec).hide}
      """
    }
    
    object StartSpec extends Specification { def is = s2"""
      ${step(println("start"))}
      """
    }
    
    class Spec1Spec extends Specification { def is = s2"""
      example1 $e1
      """
    
      def e1 = { println("example1"); ok }
    }
    
    class Spec2Spec extends Specification { def is = s2"""
      example2 $e2
      """
    
      def e2 = { println("example2"); ok }
    }
    
    object StopSpec extends Specification { def is = s2"""
      ${step(println("stop"))}
      """
    }
    

    如果你运行:

    testOnly *Suite* -- all
    

    您应该看到打印出以下几行:

    start
    example1
    example2
    stop
    

    【讨论】:

    • 感谢您的回答,我正在尝试实施类似的解决方案,但使用 BeforeAfterAll 来启动和停止应用程序。但是有一个问题,因为链接的类在应用程序启动之前首先执行
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2016-01-15
    相关资源
    最近更新 更多