Specify tests order using specs2 (scala/play frame

2019-07-24 09:32发布

问题:

I am currently writing a set of tests for a Scala Play application using the Specs2 library.

I had some Stack overflow errors during the compilation process because the tests string was too long, so I've split it into several classes.

The problem is that the tests are run simultaneously using a multi-threading process. I need to specify the order of those tests. Is there a way to do this? Regards.

回答1:

You can specify that the tests must execute sequentially by adding sequential to your specification.

If you are using unit style testing, put the statement sequential in a line above your tests (examples borrowed from specs docs):

  import org.specs2.mutable._

  class HelloWorldSpec extends Specification {

    sequential

    "The 'Hello world' string" should {
      "contain 11 characters" in {
        "Hello world" must have size(11)
      }
      "start with 'Hello'" in {
        "Hello world" must startWith("Hello")
      }
      "end with 'world'" in {
        "Hello world" must endWith("world")
      }
    }
  }

If you are using acceptance style testing, just add sequential inside the definition of is

 import org.specs2._

  class HelloWorldSpec extends Specification { def is =

    sequential                                                      ^
    "This is a specification to check the 'Hello world' string"     ^
                                                                    p^
    "The 'Hello world' string should"                               ^
      "contain 11 characters"                                       ! e1^
      "start with 'Hello'"                                          ! e2^
      "end with 'world'"                                            ! e3^
                                                                    end

    def e1 = "Hello world" must have size(11)
    def e2 = "Hello world" must startWith("Hello")
    def e3 = "Hello world" must endWith("world")
  }

As a side note, you are probably getting the stack overflow errors from an error in your software, rather than the tests being too long.



回答2:

class UsersSpec extends Specification with BeforeAll with Before {
  def is = sequential ^ s2"""

  We can create in the database
    create a user                                    $create
    list all users                                   $list
                                                     """
  import DB._

  def create = {
    val id = db.createUser("me")
    db.getUser(id).name must_== "me"
  }

  def list = {
    List("me", "you").foreach(db.createUser)
    db.listAllUsers.map(_.name).toSet must_== Set("me", "you")
  }

  // create a database before running anything
  def beforeAll = createDatabase(databaseUrl)
  // remove all data before running an example
  def before = cleanDatabase(databaseUrl)

may it helps you !



标签: scala specs2