我有一个简单的规范,其中包含几个案例:
class MySpec extends Specification {
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
现在我需要启动应用程序,运行所有测试用例,然后关闭应用程序。启动/停止应用程序非常耗时,我不希望它在每个测试用例周围发生。
我该如何在测试用例开始之前和所有测试用例完成后运行代码?
我有一个简单的规范,其中包含几个案例:
class MySpec extends Specification {
"Something" should {
"case 1" in {
...
}
"case 2" in {
...
}
}
}
现在我需要启动应用程序,运行所有测试用例,然后关闭应用程序。启动/停止应用程序非常耗时,我不希望它在每个测试用例周围发生。
我该如何在测试用例开始之前和所有测试用例完成后运行代码?
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
,并实现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 {
...
}
}
}
好的,正如我在评论中提到的那样,我曾经遇到过这个问题。我需要测试未过滤的端点,而对于每个规范来说,最好的方法是启动一个仅有一个端点的未过滤服务器,运行规范,然后关闭服务器。为了实现这一目标,我首先定义了一个类似于以下代码的基本规范:
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...
虽然不是完美的,但它起作用了。有没有另一种(可能更简洁/更好)的方法来做到这一点?可能有,但这是一个解决方案,你可以考虑使用。
BeforeAfterAll
trait。覆盖它将提供所需的功能。例如,测试代码: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
好的,这是一个老问题,但它可能会帮助到某些人。
我正在使用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")
}
}