OneAppPerSuite的替代方案在specs2 Scala测试中是什么?

3
我正在使用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")
    }
  }
}

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

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

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。这里是链接和示例。
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中是否可以实现类似的功能?

1个回答

1

使用specs2,您可以通过规范引用执行类似操作:

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来启动和停止应用程序。但是有一个问题,链接的类在应用程序启动之前就被执行了。 - perfectus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接