1

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

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

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

4

2 回答 2

6

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

如果您正在使用单元样式测试,请将语句sequential放在测试上方的一行中(示例来自 specs 文档):

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

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

于 2012-11-19T18:03:21.213 回答
1
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)

愿它对你有帮助!

于 2016-01-27T10:52:48.917 回答