Specs2设置整套西装前后的环境

叮咚

我正在为使用dynamodb的spray.io项目编写一些specc2集成测试。我正在使用sbt-dynamodb将本地的dynamodb加载到环境中。在运行测试之前,我使用以下模式加载表。

trait DynamoDBSpec extends SpecificationLike {

  val config = ConfigFactory.load()

  val client = new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain())

  lazy val db = {
    client.setEndpoint(config.getString("campaigns.db.endpoint"))
    new DynamoDB(client)
  }


  override def map(fs: =>Fragments): Fragments =
    Step(beforeAll) ^ fs ^ Step(afterAll)

  protected def beforeAll() = {
    //load my tables
  }

  protected def afterAll() = {
    //delete my tables
  }
}

然后,可以使用DynamoDBSpec扩展任何测试类,然后将创建表。一切正常,直到从多个测试类扩展DynamoDBSpec,在此期间它引发ResourceInUseException:“无法创建现有表”。原因是它们并行执行,因此它想同时执行表创建。

我试图通过在顺序模式下运行测试来克服它,但是beforeall和afterall仍然并行执行。

理想情况下,我认为最好在整个套件运行之前创建表,而不是每次调用Spec类,然后在整个套件完成后将它们拆除。有谁知道如何做到这一点?

埃里克

有两种方法可以实现此目的。

带物体

您可以使用一个对象来同步数据库的创建

object Database {
  lazy val config = ConfigFactory.load()

  lazy val client = 
    new AmazonDynamoDBClient(new DefaultAWSCredentialsProviderChain())

  // this will only be done once in
  // the same jvm
  lazy val db = {
    client.setEndpoint(config.getString("campaigns.db.endpoint"))
    val database = new DynamoDB(client)
    // drop previous tables if any 
    // and create new tables
    database.create...
    database
  }
}

// BeforeAll is a new trait in specs2 3.x
trait DynamoDBSpec extends SpecificationLike with BeforeAll {
  //load my tables
  def beforeAll = Database.db
}

如您所见,在此模型中,我们不会在规范完成时删除表(因为我们不知道是否已执行所有其他规范),而是在重新运行规范时才删除表。这实际上可能是一件好事,因为这将帮助您调查故障(如果有)。

在全局级别同步规范并最终进行适当清理的另一种方法是使用规范链接。

带链接

使用specs2 3.3,您可以使用创建规范之间的依赖关系links这意味着您可以定义“套件”规范,该规范将用于:

  1. 启动数据库
  2. 收集所有相关规范
  3. 删除数据库

例如

import org.specs2._
import specification._
import core.Fragments
import runner.SpecificationsFinder

// run this specification with `all` to execute
// all linked specifications
class Database extends Specification { def is =
  "All database specifications".title ^ br ^
  link(new Create).hide ^
  Fragments.foreach(specs)(s => link(s) ^ br) ^
  link(new Delete).hide

  def specs = specifications(pattern = ".*Db.*")
} 

// start the database with this specification
class Create extends Specification { def is = xonly ^ 
  step("create database".pp)
}

// stop the database with this specification
class Delete extends Specification {  def is = xonly ^
  step("delete database".pp)
}

// an example of a specification using the database
// it will be invoked by the "Database" spec because 
// its name matches ".*Db.*"
class Db1Spec extends Specification { def is = s2"""
   test $db
   """
  def db = { println("use the database - 1"); ok }
}
class Db2Spec extends Specification { def is = s2"""
   test $db
   """
  def db = { println("use the database - 2"); ok }
}

运行时:

sbt> test-only *Database* -- all

您应该会看到类似

create database
use the database - 1
use the database - 2
delete database 

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Specs2 IT环境拆解

来自分类Dev

符合specs2的地图

来自分类Dev

播放+ specs2 + scalacheck?

来自分类Dev

具有执行环境的Specs2 ForEach上下文

来自分类Dev

使用“在新的WithApplication中”时如何在specs2中进行设置/拆卸

来自分类Dev

Specs2“值in不是String的成员”

来自分类Dev

Specs2 JsonMatchers匹配“空数组”

来自分类Dev

使用Specs2测试Akka

来自分类Dev

specs2运行特定测试

来自分类Dev

Play Framework specs2失败

来自分类Dev

specs2:如何使用“ failtrace”选项

来自分类Dev

使用Specs2测试Akka

来自分类Dev

如何跳过使用specs2的实现

来自分类Dev

travis 中的 specs2 html 输出

来自分类Dev

Scala Specs2 Matchers与“ aka”不起作用

来自分类Dev

Specs2 + Scalacheck测试由于许多废弃而失败

来自分类Dev

从specs2中的隐式类模拟方法

来自分类Dev

在Specs2测试块中添加消息

来自分类Dev

升级后无法使用specs2编译ScalaCheck

来自分类Dev

如何使用specs2对测试进行分组?

来自分类Dev

Scala specs2与上下文嵌套

来自分类Dev

将Specs2结果与Jenkins集成

来自分类Dev

specs2 After方法在示例之前运行

来自分类Dev

在Specs2中使用Akka TestKit

来自分类Dev

如何回滚Slick 3 + Specs2的集成测试?

来自分类Dev

在Specs2 / Mockito中使用Hamcrest hasProperty

来自分类Dev

Specs2测试无法在IntelliJ中本机运行

来自分类Dev

containsTheSameElementsAs如何在specs2中工作

来自分类Dev

Specs2:跳过继承的测试套件