org.scalatest:全局设置(如beforeAllSuites?) [英] org.scalatest: Global setup (like beforeAllSuites?)

查看:105
本文介绍了org.scalatest:全局设置(如beforeAllSuites?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用org.scalatest进行测试的scala应用程序.这些测试需要一些全局设置(和拆卸),以便管理测试数据库.

I have a scala application with some test using org.scalatest. These test need some global setup (and teardown), in order to manage the test database.

请不要告诉我我的测试不应该访问数据库,而应该使用Java-DAO-Stub-WTF-Overkill-Way™:-).

Please don't tell me my tests should not hit the database and I should do it the Java-DAO-Stub-WTF-Overkill-Way™ :-).

我正在使用SBT运行测试,该测试提供了一种在测试前后执行代码的方式:

I'm running the tests using SBT, which provides a way to execute code before and after test:

    testOptions in Test += Tests.Setup( () => println("Setup") )

    testOptions in Test += Tests.Cleanup( () => println("Cleanup") )

不幸的是,我不能在那里访问有问题的课程.毫不奇怪,将它们导入build.sbt也不起作用.

Unfortunately I cannot access the classes in question there. Unsurprisingly, importing them into build.sbt does not work either.

有什么想法吗?

推荐答案

您可以使用 BeforeAndAfterAll BeforeAndAfter 特质,取决于您的需求.

You can use the BeforeAndAfterAll or BeforeAndAfter traits, depending upon your needs.

BeforeAndAfterAll:

BeforeAndAfterAll:

可以组合到需要先调用方法的套件中的特质 和执行套件后.此特征允许执行代码 在套件的所有测试和嵌套套件之前和/或之后 运行.

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,以扩展此特征.

So in this instance, you would define a MasterSuite which contains all other Suites/Tests, which extends this trait.

之前和之后:

可以组合到需要在之前和之后执行代码的套件中的特质 在运行每个测试之后.此特征有助于在测试中进行测试 实例变量中包含哪些可变夹具对象被替换 或在每个测试或套件之前重新初始化.

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.

这篇关于org.scalatest:全局设置(如beforeAllSuites?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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