我有许多测试文件,每个都有自己的测试。到目前为止,每个人都有一个特征,我在其中创建一个配置对象。
现在构建该对象需要花费两分钟的时间,因为它需要做很多工作来调用大量数据库(不要问 - 真的,必须要这样做)。
有没有办法在多个文件中的多个测试套件之间共享此对象(下面的tldConfigMap),而无需反复构建它?
以下是我的做法 - 正如您所看到的,当作为特征引入时,每次都会调用load():
trait TLDMapAcceptanceIsLoadedSpec extends org.scalatest.fixture.FlatSpecLike with TLDConfigMap {
val tldConfigMap: Map[String, TLDConfig] = load(withAttributes = true).right.get
type FixtureParam = Map[String, TLDConfig]
def withFixture(test: OneArgTest) = {
withFixture(test.toNoArgTest(tldConfigMap)) // "loan" the fixture to the test
}
}
答案 0 :(得分:1)
您可以将其放入object
(假设它确实是配置而不是通过测试更改):
object TldConfigMap {
val tldConfigMap: Map[String, TLDConfig] = load(withAttributes = true).right.get
}
trait TLDMapAcceptanceIsLoadedSpec extends org.scalatest.fixture.FlatSpecLike with TLDConfigMap {
def tldConfigMap: Map[String, TLDConfig] = TldConfigMap.tldConfigMap // or just use it directly
type FixtureParam = Map[String, TLDConfig]
def withFixture(test: OneArgTest) = {
withFixture(test.toNoArgTest(tldConfigMap)) // "loan" the fixture to the test
}
}