【问题标题】:Execute code before and after specification在规范之前和之后执行代码
【发布时间】:2013-05-31 23:15:44
【问题描述】:

我有几个案例的简单说明:

class MySpec extends Specification {

  "Something" should {

    "case 1" in {
      ...
    }

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

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

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

【问题讨论】:

  • @om-nom-nom 它只解释了如何围绕每个案例执行代码。
  • 哎呀,我的意思是TemplateGlobal setup/teardown 段落(带有数据库设置/清理的段落):-)
  • @om-nom-nom 我已经有了。它只解释了如何在规范之前执行代码。
  • 我实际上遇到了同样的情况,并且没有发现任何开箱即用的功能之前/之后都足以满足我的需求。如果你能在那里呆上大约一个小时(因为我正在上班途中),我会发布我的解决方案。

标签: scala testing specs2


【解决方案1】:

我根据 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()
}

然后在Specification 中混合BeforeAllAfterAll 并实现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 {
      ...
    }
  }
}

【讨论】:

  • 这真的很有帮助,谢谢。现在,如果我想为所有规范创建一个全局设置怎么办?
【解决方案2】:

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

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...

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

【讨论】:

  • 谢谢!我已经根据您的解决方案开发了更清洁的解决方案,并将其作为答案发布。
【解决方案3】:

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

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

【讨论】:

    【解决方案4】:

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

    我正在使用 Play 框架。在我的测试中,我使用了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")
      }
    }
    

    【讨论】:

    • AFAIU 这个问题是关于 specs2,而这个答案是关于 scalatest。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-12-25
    • 2021-10-05
    相关资源
    最近更新 更多