ScalaTest——Fixtures

缺乏、安全感 2022-05-12 15:10 312阅读 0赞

Fixture翻译成中文有这么些意思:固定装置;卡具;固定附物,固定附着物;固定财产,在ScalaTest中,可能会有这么一种情境:在一个测试类中,不同的测试方法需要的类实例、依赖等数据是一样的,显然,没必要为每个测试类去new一些它们专用的数据,可以提供一些公共的数据,然后在不同的测试方法中重用它们。

要做到数据的重用,有很多方法:

  • Scala语言自带的方法
  • ScalaTest测试框架提供的解决方案
  • 每一种测试方法也有自己的一些实现
  • JUnitTestNG也有它们自己的结构
8.1 匿名对象

先从Scala语言本身提供的方案说起。Scala语言提供的匿名对象可以用来解决前面说到的数据重用的问题。Scala中的匿名对象就是没有名字的对象,匿名对象一旦被创建,就可以在不同的测试方法中重用。每次匿名对象被请求的时候,它都会创建一个全新的对象。如下面的例子:

  1. import org.scalatest.Matchers
  2. import org.scalatest.FunSpec
  3. class AlbumFixtureSpec extends FunSpec with Matchers {
  4. def fixture = new {
  5. val letterFromHome = new Album("Letter from Home", 1989, new Band("Pat Metheny Group"))
  6. }
  7. describe("The Letter From Home Album by Pat Metheny") {
  8. it("should get the year 1989 from the album") {
  9. val album = fixture.letterFromHome
  10. album.year should be (1989)
  11. }
  12. }
  13. }

上面的例子定义了一个fixture方法来获取一个Album对象,fixture方法每次被调用都会产生一个匿名对象

这里有一点需要注意的,即使fixture方法产生的是一个可变mutable的对象,在另一个方法调用fixture时,它仍然后产生一个新的对象,而不是提供之前的对象。下面的例子使用了可变集合来说明:

  1. import org.scalatest.FunSpec
  2. import org.scalatest.Matchers
  3. class AlbumMutableFixtureSpec extends FunSpec with Matchers {
  4. def fixture = new {
  5. import scala.collection.mutable._
  6. val beachBoys = new Band("Beach Boys")
  7. val beachBoysDiscography = new ListBuffer[Album]()
  8. beachBoysDiscography += (new Album("Surfin' Safari", 1962, beachBoys))
  9. }
  10. describe("Given a single fixture each beach boy discography initially contains a single album") {
  11. it("then after 1 album is added, the discography size should have 2") {
  12. val discographyDeBeachBoys = fixture.beachBoysDiscography
  13. discographyDeBeachBoys += (new Album("Surfin' Safari", 1962, fixture.beachBoys))
  14. discographyDeBeachBoys.size should be(2)
  15. }
  16. it("then after 2 albums are added, the discography size should return 3") {
  17. val discographyDeBeachBoys = fixture.beachBoysDiscography
  18. discographyDeBeachBoys += (new Album("Surfin' Safari", 1962, fixture.beachBoys))
  19. discographyDeBeachBoys += (new Album("All Summer Long", 1964, fixture.beachBoys))
  20. discographyDeBeachBoys.size should be(3)
  21. }
  22. }
  23. }

跟前一个例子一样,上面的例子使用了fixture方法,在Scala语言中,使用def定义的方法在每次被调用的时候都会重新执行方法体。因而,在每个测试方法中我们得到的都是新的实例。

8.2 Fixture Traits

另一种在ScalaTest中的可供选择的做法是自定义一个特质来确保每个测试方法都得到不同的对象。特质在混入对象后仍然后持有它原来的方法,并不会在混入的对象之中共享。下面的例子使用一个特质而不是一个匿名对象

  1. import org.scalatest.Matchers
  2. import org.scalatest.FunSpec
  3. class AlbumFixtureTraitSpec extends FunSpec with Matchers {
  4. trait AlbumFixture {
  5. val letterFromHome = new Album("Letter from Home", 1989, new Band("Pat Metheny Group"))
  6. }
  7. describe("The Letter From Home Album by Pat Metheny") {
  8. it("should get the year 1989 from the album") {
  9. new AlbumFixture {
  10. letterFromHome.year should be(1989)
  11. }
  12. }
  13. }
  14. }

上面的例子使用了一个特质来封装测试方法需要的数据,在特质中又使用了匿名对象的方式来创建对象,实际上,这种实现方式依然使用了Scala的语言特性。

8.3 OneInstancePerTest

除了依赖Scala的语言特性,ScalaTest也提供了方法来确保每个测试都有它自己的数据实例。下面的例子使用了OnInstancePerTest特质来实现:

  1. import org.scalatest.Matchers
  2. import collection.mutable.ListBuffer
  3. import org.scalatest.{FreeSpec, OneInstancePerTest}
  4. class AlbumListOneInstancePerTestFreeSpec extends FreeSpec with Matchers
  5. with OneInstancePerTest {
  6. val graceJonesDiscography = new ListBuffer[Album]()
  7. graceJonesDiscography += (new Album("Portfolio", 1977, new Artist("Grace", "Jones")))
  8. "Given an initial Grace Jones Discography" - {
  9. "when an additional two albums are added, then the discography size should be 3" in {
  10. graceJonesDiscography += (new Album("Fame", 1978, new Artist("Grace", "Jones")))
  11. graceJonesDiscography += (new Album("Muse", 1979, new Artist("Grace", "Jones")))
  12. graceJonesDiscography.size should be(3)
  13. }
  14. "when one additional album is added, then the discography size should be 2" in {
  15. graceJonesDiscography += (new Album("Warm Leatherette", 1980, new Artist("Grace", "Jones")))
  16. graceJonesDiscography.size should be(2)
  17. }
  18. }
  19. "Given an initial Grace Jones Discography " - {
  20. "when one additional album from 1980 is added, then the discography size should be 2" in {
  21. graceJonesDiscography += (new Album("Nightclubbing", 1981, new Artist("Grace", "Jones")))
  22. graceJonesDiscography.size should be(2)
  23. }
  24. }
  25. }

上面的例子使用了FreeSpec风格的测试写法。在测试开始时,定义了graceJonesDiscography变量,然后该变量被用在多个测试中。由于AlbumListOneInstancePerTestFreeSpec类混入了OneInstancePerTest接口,graceJonesDiscography变量在每个测试方法中使用时都会被重新创建。

上面的例子中,测试方法是在in代码块中的内容。

8.4 Before and After

为了更好的控制在测试方法执行前、后有什么行为,ScalaTest提供了一个名为BeforeAndAfter的特质。可以很方便的指定在每一个测试方法执行前有什么行为,在每个测试方法执行后有什么行为。如下面的例子:

  1. import collection.mutable.ListBuffer
  2. import org.scalatest.{BeforeAndAfter, WordSpec}
  3. import org.scalatest.Matchers
  4. class AlbumBeforeAndAfterFixtureSpec extends WordSpec with Matchers with BeforeAndAfter {
  5. val humanLeagueDiscography = new ListBuffer[Album]()
  6. before {
  7. info("Starting to populate the discography")
  8. humanLeagueDiscography += (new Album("Dare", 1981, new Band("Human League")))
  9. }
  10. "A mutable ListBuffer of albums" should {
  11. "have a size of 3 when two more albums are added to the Human League Discography" in {
  12. humanLeagueDiscography += (new Album("Hysteria", 1984, new Band("Human League")))
  13. humanLeagueDiscography += (new Album("Crash", 1986, new Band("Human League")))
  14. humanLeagueDiscography should have size (3)
  15. }
  16. "have a size of 2 when one more album is added to the Human League Discography" in {
  17. humanLeagueDiscography += (new Album("Romantic", 1990, new Band("Human League")))
  18. humanLeagueDiscography should have size (2)
  19. }
  20. }
  21. after {
  22. info("Clearing the discography")
  23. humanLeagueDiscography.clear()
  24. }
  25. }

上面的例子使用了WordSpec风格的测试,在测试方法执行前,初始化了一个名为humanLeagueDiscography的可变列表,在测试方法执行完毕后,humanLeagueDiscography可变列表被清空。BeforeAndAfter特质中的beforeafter方法和在JUnit中被标记为BeforeAfter的方法作用是一样的。

上面的例子中,测试方法依然是在in代码块中的内容。

发表评论

表情:
评论列表 (有 0 条评论,312人围观)

还没有评论,来说两句吧...

相关阅读