我有简单的说明,其中有几个案例:
class MySpec extends Specification {
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
现在我需要启动应用程序,运行所有情况并关闭应用程序。启动/停止应用程序非常耗时,我不希望它发生在每个案例周围。
如何在案例开始之前和所有案例结束后运行代码?
答案 0 :(得分:29)
我已经提出了基于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()
}
然后在BeforeAllAfterAll
中混合Specification
并实施beforeAll
和afterAll
方法:
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 {
...
}
}
}
答案 1 :(得分:7)
好的,正如我的评论中所提到的,我实际上也有同样的问题。我需要测试未过滤的端点,每个规范的最佳方法是使用单个端点启动Unfiltered服务器,运行规范然后关闭服务器。为此,我首先定义了一个类似于此的基本规范:
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...
它并不完美,但它确实有效。还有另一种(可能更清洁/更好)的方法吗?可能,但这是您可以使用的一种解决方案。
答案 2 :(得分:1)
好的,这是一个老问题,但它可能对某人有帮助。
我正在使用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")
}
}
答案 3 :(得分:0)
现有的答案很不错,但是现在Series.str.contains
中有一个简单的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