在所有 Scalatest 测试之前或之后做一些事情 [英] Doing something before or after all Scalatest tests

查看:48
本文介绍了在所有 Scalatest 测试之前或之后做一些事情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一套可测试 RESTful API 不同端点的 Scalatest 测试.我真的希望将它们分成不同的文件以实现最佳组织.

I have a suite of scalatest tests that test different endpoints of a RESTful API. I really want them separated into different files for best organization.

我的问题是如何在所有测试之前启动一些东西(在我的例子中是一个 HTTP 服务器,但它是什么并不重要)并在所有测试完成后关闭它.

My problem is how to start something (an HTTP server in my case, but it doesn't matter what it is) before all the tests and shut it down after all the tests are done.

我知道BeforeAndAfterAll,但这只能在一个测试文件中完成before/after.我需要类似的东西,但对于所有测试,例如:

I know about BeforeAndAfterAll, but that only accomplishes before/after inside one test file. I need something like that, but for all tests, for example:

-- 在测试前启动 http 服务器
-- 运行所有测试套件
-- 关闭http服务器

-- start http server before tests
-- run all test suites
-- shut down http server

推荐答案

执行此操作的预期方法是使用嵌套套件.Suite 有一个 NestedSuites 方法,它返回一个 IndexedSeq[Suite](在 2.0 中,在 1.9.1 中它是一个 List[Suite]).Suite 还有一个 runNestedSuites 方法,负责执行任何嵌套的套件.默认情况下,runNestedSuites 调用nestedSuites,并且在每个返回的套件上直接调用run,或者如果传递了分发器,则将嵌套套件放入分发器中,以便它们可以并行运行.

The intended way to do this is to use nested suites. Suite has a nestedSuites method that returns an IndexedSeq[Suite] (in 2.0, in 1.9.1 it was a List[Suite]). Suite also has a runNestedSuites method that is responsible for executing any nested suites. By default runNestedSuites calls nestedSuites, and on each returned Suite either invokes run directly, or if a Distributor is passed, puts the nested suites in the distributor so that they can be run in parallel.

所以你真正可能想做的是将 Foo 和 Bar 变成类,并从 EndpointTests 的 NestedSuites 方法返回它们的实例.有一个类可以使这变得容易,称为套件.以下是其使用示例:

So what you really probably want to do is make Foo and Bar into classes, and return instances of them from the nestedSuites method of EndpointTests. There's a class that makes that easy called Suites. Here's an example of its use:

import org.scalatest._
import matchers.MustMatchers

class Foo extends FunSpec with MustMatchers {
  describe("Message here...") {
    it("Must do something") {  }
    it("Must be ok") {  }
  }
}

class Bar extends FunSpec with MustMatchers {
  describe("Hello you...") {
    it("One more!") {  }
  }
}

class EndpointTests extends Suites(new Foo, new Bar) with BeforeAndAfterAll {

  override def beforeAll(configMap: Map[String, Any]) {
    println("Before!")  // start up your web server or whatever
  }     

  override def afterAll(configMap: Map[String, Any]) {
    println("After!")  // shut down the web server
  }         
}

但是,一个潜在的问题是,如果您使用发现来查找要运行的套件,那么 EndpointTest、Foo 和 Bar 中的所有三个都将被发现.在 ScalaTest 2.0 中,您可以使用 @DoNotDiscover 注释 Foo 和 Bar,而 ScalaTest 的 Runner 不会发现它们.但是 sbt 仍然会.我们目前正在增强 sbt,以便它忽略使用 DoNotDiscover 注释的其他可发现的套件,但这将在 sbt 0.13 中出现,该版本尚未发布.同时,您可以通过向 Foo 和 Bar 添加未使用的构造函数参数来让 sbt 忽略它们.

One potential problem, though, is that if you are using discovery to find Suites to run, all three of EndpointTests, Foo, and Bar will be discovered. In ScalaTest 2.0 you can annotate Foo and Bar with @DoNotDiscover, and ScalaTest's Runner will not discover them. But sbt still will. We are currently enhancing sbt so that it passes over otherwise discoverable Suites that are annotated with DoNotDiscover, but this will be in sbt 0.13, which isn't out yet. In the meantime you can get sbt to ignore them by adding an unused constructor parameter to Foo and Bar.

这篇关于在所有 Scalatest 测试之前或之后做一些事情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆