scala - specs2 scala 测试中 OneAppPerSuite 的替代方案

标签 scala unit-testing testing playframework-2.0 specs2

我正在使用 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 中实现类似的东西吗?

最佳答案

使用 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

关于scala - specs2 scala 测试中 OneAppPerSuite 的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37891820/

相关文章:

generics - 如何获取 Scala 中类型的默认值?

java - 如何验证调用了非模拟对象的方法?

java - 在 jUnit 中测试有序的 CRUD

c# - 在 C# 中获取 Selenium RemoteWebDriver 的 session ID

ios - 建议在不使用 MacOS 或 Xcode 的情况下在 iOS(最好是 9 及更高版本)上启用开发人员选项的任何解决方法?

rest - 如何在 Postman 测试中读取环境变量?

Scala:为什么我必须在这里加上额外的括号?

scala - 如何用akka和sbt开发?

java - 应该玩 2 xx 应用程序同时具有 build.sbt 和 build.scala

.net - 自定义 WCF 扩展的单元测试,例如自定义行为和检查器