我有一个使用 org.scalatest 进行一些测试的 Scala 应用程序。这些测试需要一些全局设置(和拆卸),以便管理测试数据库。
请不要告诉我我的测试不应该访问数据库,我应该使用 Java-DAO-Stub-WTF-Overkill-Way™ :-)。
我正在使用 SBT 运行测试,它提供了一种在测试前后执行代码的方法:
testOptions in Test += Tests.Setup( () => println("Setup") )
testOptions in Test += Tests.Cleanup( () => println("Cleanup") )
不幸的是,我无法访问那里的相关类(class)。不出所料,将它们导入 build.sbt 也不起作用。
有任何想法吗?
最佳答案
您可以使用 BeforeAndAfterAll或 BeforeAndAfter特点,看你的需求。
之前和之后:
Trait that can be mixed into suites that need methods invoked before and after executing the suite. This trait allows code to be executed before and/or after all the tests and nested suites of a suite are run.
因此,在这种情况下,您将定义一个包含所有其他套件/测试的 MasterSuite,它扩展了此特征。
之前和之后:
Trait that can be mixed into suites that need code executed before and after running each test. This trait facilitates a style of testing in which mutable fixture objects held in instance variables are replaced or reinitialized before each test or suite.
关于installation - org.scalatest : Global setup (like beforeAllSuites? ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8486869/