首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >OneAppPerSuite在specs2 scala测试中的应用

OneAppPerSuite在specs2 scala测试中的应用
EN

Stack Overflow用户
提问于 2016-06-17 23:39:47
回答 1查看 213关注 0票数 3

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

代码语言:javascript
复制
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")
    }
  }
}

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

我从链接那里找到了一个解决办法。对于每个测试类,应用程序只初始化一次(我还没有测试过)。

代码语言:javascript
复制
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())
}

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

代码语言:javascript
复制
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实现了类似的解决方案。下面是链接和示例。

代码语言:javascript
复制
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?中实现类似的东西

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-18 21:31:39

使用specs2,您可以对规格参考做类似的事情

代码语言:javascript
复制
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"))}
  """
}

如果你跑:

代码语言:javascript
复制
testOnly *Suite* -- all

您应该看到打印出的下列行:

代码语言:javascript
复制
start
example1
example2
stop
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37891820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档