使用specs2指定测试顺序(scala / play框架)

时间:2012-11-19 16:37:00

标签: scala specs2

我目前正在使用Specs2库为Scala Play应用程序编写一组测试。

我在编译过程中遇到了一些Stack溢出错误,因为测试字符串太长了,所以我把它分成了几个类。

问题是测试是使用多线程进程同时运行的。我需要指定那些测试的顺序。有没有办法做到这一点?问候。

2 个答案:

答案 0 :(得分:5)

您可以通过将sequential添加到规范中来指定测试必须按顺序执行。

如果您正在使用单元样式测试,请将语句sequential放在测试上方的一行(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")
      }
    }
  }

如果您正在使用验收方式测试,只需在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")
  }

作为附注,您可能会从软件中的错误中获得堆栈溢出错误,而不是测试太长。

答案 1 :(得分:0)

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)

它可以帮到你!