ScalaTest 中的夹具对象和清理

Fixture objects and cleanup in ScalaTest

我试图从这个 link.

中了解 Scala 中用于 BDD 测试的固定对象

get-fixture methods/fixture-context objects - link 表示只有在不需要清理时才推荐使用这两种方式。

get-fixture methods - The extract method refactor helps you create a fresh instances of mutable fixture objects in each test that needs them, but doesn't help you clean them up when you're done.

fixture-context objects - By placing fixture methods and fields into traits, you can easily give each test just the newly created fixtures it needs by mixing together traits. Use this technique when you need different combinations of mutable fixture objects in different tests, and don't need to clean up after.

既然 get-fixture 方法和 fixture 上下文对象无论如何都会提供测试数据的新实例,那么测试数据的清理是否会自动处理?清理在这方面意味着什么?对清理的引用是指垃圾收集还是指由一个测试对另一个测试产生不利影响的夹具对象中的数据?

垃圾收集总是完成的。清理可能意味着:

  • 关闭打开的文件
  • 关闭任何打开的流
  • 正在删除创建的文件
  • 正在与数据库断开连接
  • 基本上任何 activity 确保某些状态恢复到以前的方式