Specify tests order using specs2 (scala/play framework)

不羁的心 提交于 2019-12-11 05:38:28

问题


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 !



来源:https://stackoverflow.com/questions/13458194/specify-tests-order-using-specs2-scala-play-framework

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!