14

我有几个案例的简单规范:

class MySpec extends Specification {

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}

现在我需要启动应用程序,运行所有案例,然后关闭应用程序。启动/停止应用程序非常耗时,我不希望它在每种情况下都发生。

如何在案例开始之前和所有案例完成之后运行代码?

4

4 回答 4

31

我根据 cmbaxter 的回答提出了以下解决方案。

import org.specs2.specification.Step

trait BeforeAllAfterAll extends Specification {
  // see http://bit.ly/11I9kFM (specs2 User Guide)
  override def map(fragments: =>Fragments) = 
    Step(beforeAll) ^ fragments ^ Step(afterAll)

  protected def beforeAll()
  protected def afterAll()
}

然后混入BeforeAllAfterAllSpecification实现beforeAllafterAll方法:

class MySpec extends Specification with BeforeAllAfterAll {

  def beforeAll() {
    println("Doing setup work...")
  }

  def afterAll() {
    println("Doing shutdown work...")
  }

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}

最后,提取初始化以在规范之间共享它:

trait InApplication extends BeforeAllAfterAll {
  def beforeAll() {
    println("Doing setup work...")
  }

  def afterAll() {
    println("Doing shutdown work...")
  }
}

class MySpec extends Specification with InApplication {

  "Something" should {

    "case 1" in {
      ...
    }

    "case 2" in {
      ...
    }
  }
}
于 2013-06-06T02:45:29.280 回答
7

好的,正如我在评论中提到的,我实际上遇到了同样的问题。我需要测试未过滤的端点,每个规范的最佳方法是使用单个端点启动未过滤的服务器,运行规范然后关闭服务器。为此,我首先定义了一个与此类似的基本规范:

import org.specs2.mutable.Specification

abstract class SpecificationBase extends Specification{
  //Do setup work here
  step{
    println("Doing setup work...")
    success
  }

  //Include the real spec from the derived class
  include(spec)

  //Do shutdown work here
  step{
    println("Doing shutdown work...")
    success
  }  

  /**
   * To be implemented in the derived class.  Returns the real specification
   * @return Specification
   */
  def spec:Specification
}

基本上,这个基类将完整的规范组装为设置步骤和拆卸步骤,而真正的规范(在具体规范类中定义)夹在中间。所以使用这个基类的测试看起来像这样:

class MySpec extends SpecificationBase{ def spec = 
  new Specification{
    "A request to do something" should{
      "be successful in case 1" in {
        println("Testing case 1")
        success
      }
      "be successful in case 2" in {
        println("Testing case 2")
        success
      }      
    }
  }
}

当你运行它时,你会看到:

Doing setup work...
Testing case 1
Testing case 2
Doing shutdown work...

它并不完美,但它有效。是否有另一种(可能更清洁/更好)的方法来做到这一点?可能,但这是您可以考虑使用的一种解决方案。

于 2013-06-05T12:03:53.323 回答
1

现有的答案很好,但现在specs2BeforeAfterAll有一个简单的特征。覆盖它将提供所需的功能。例如,测试:

class ASpec extends Specification with BeforeAfterAll {

  "The 'Hello world' string" should {
    "contain 11 characters" in {
      println("test 1")
      "Hello world" must have size (11)
    }
    "start with 'Hello'" in {
      println("test 2")
      "Hello world" must startWith("Hello")
    }
    "end with 'world'" in {
      println("test 3")
      "Hello world" must endWith("world")
    }
  }

  def beforeAll(): Unit = {
    println("beforeAll")
  }

  def afterAll(): Unit = {
    println("afterAll")
  }
}

将输出:

beforeAll
test 3
test 2
test 1
afterAll
于 2020-12-07T11:06:57.370 回答
-1

好的,这是一个老问题,但它可能会对某人有所帮助。

我正在使用播放框架。在我的测试中,我使用org.scalatest.BeforeAndAfterAll

例子:

import org.scalatest.BeforeAndAfterAll

class MySpec extends PlaySpec with BeforeAndAfterAll {

  "Some test" must {
    "print a text" in {

      println("Some test")
      2 mustBe 2
    }
  }

  override def beforeAll(): Unit = {
    println("Before")
  }

  override def afterAll(): Unit = {
    println("After")
  }
}
于 2017-04-05T20:33:26.240 回答