【发布时间】:2012-11-01 10:12:56
【问题描述】:
是否有一些首选的方法来设计 Specs2 测试,其中有很多测试取决于以前的测试结果?
在下面,您会找到我当前的测试套件。我不喜欢测试片段之间的vars。但它们是“需要的”,因为某些测试会生成 ID 号,后续测试会重复使用。
我应该将 ID 号存储在 Specs2 上下文中,还是创建一个单独的对象来保存所有可变状态?并且只在规范对象中放置测试片段?还是有更好的方法?
如果测试失败,我想取消相同深度的剩余测试。我可以让测试片段相互依赖吗? (我知道我可以在单个测试片段中取消剩余的匹配器(通过使用可变测试,或通过 orSkip),但是取消整个片段呢?)
.
object DatabaseSpec extends Specification {
sequential
"The Data Access Object" should {
var someId = "" // These var:s feels error prone, is there a better way?
"save an object" >> {
someId = database.save(something)
someId must_!= ""
// I'd like to cancel the remaining tests, below, at this "depth",
// if this test fragmen fails. Can I do that?
// (That is, cancel "load one object", "list all objects", etc, below.)
}
"load one object" >> {
anObject = database.load(someId)
anObject.id must_== someId
}
"list all objects" >> {
objs = database.listAll()
objs.find(_.id == someId) must beSome
}
var anotherId = ""
...more tests that create another object, and
...use both `someId` and `anotherId`...
var aThirdId = ""
...tests that use `someId`, `anotherId` and `aThirdId...
}
"The Data Access Object can also" >> {
...more tests...
}
}
【问题讨论】:
-
BTW specs2 3.x 旨在解决这个确切的问题,您可以根据以前的测试结果创建任意测试。见这里:etorreborre.github.io/specs2/guide/SPECS2-3.1.1/…
标签: scala integration-testing specs2