Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute code before and after specification

I have simple specification with several cases in it:

class MySpec extends Specification {

  "Something" should {

    "case 1" in {
      ...
    }

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

Now I need to start application, run all cases, and shutdown the application. Starting/stopping the application is time consuming and I don't want it to happen around each case.

How do I run code before cases are started and after all the cases have finished?

like image 503
lambdas Avatar asked Jun 05 '13 09:06

lambdas


1 Answers

The existing answers are great, but now there is a simple BeforeAfterAll trait in specs2. Overriding it will provide the wanted functionality. For instance, the test:

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")
  }
}

Will output:

beforeAll
test 3
test 2
test 1
afterAll
like image 193
Tomer Shetah Avatar answered Sep 21 '22 21:09

Tomer Shetah