在所有测试运行之前仅初始化一次值
initializate value only once before all tests runs
我只想在所有测试之前初始化一次值 运行s
现在值 insertDbScheme
在每个测试中创建方案,如果可能的话 运行 它只创建一次。从 sbt 任务插入方案不合适
我有两个测试
测试 1:
class Test1 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
测试 2:
class Test2 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
和一个将方案插入数据库的基本特征
trait InsertDbScheme {
lazy val insertDbScheme = {
println("insert scheme to database")
}
}
运行宁测试的结果
insert scheme to database
insert scheme to database
[info] Test1
[info]
[info] + test
[info]
[info]
[info] Total for specification Test1
[info] Finished in 152 ms
[info] 1 example, 0 failure, 0 error
[info]
[info] Test2
[info]
[info] + test
[info]
[info]
[info] Total for specification Test2
[info] Finished in 152 ms
[info] 1 example, 0 failure, 0 error
[info]
[info] ScalaCheck
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 12 s, completed May 18, 2017 6:35:21 PM
您可以使用 object
来确保它的 lazy val
只被实例化一次:
trait InsertDbScheme {
import InsertDbScheme._
lazy val insertDbScheme = insertDbSchemeOnce
}
object InsertDbScheme {
lazy val insertDbSchemeOnce = {
println("insert scheme to database")
}
}
最简单的方法是在 InsertDbScheme
对象中使用变量:
trait InsertDbScheme {
def insertDbScheme = synchronized {
if (!InsertDbScheme.done) {
println("insert scheme to database")
InsertDbScheme.done = true
}
}
}
object InsertDbScheme {
var done = false
}
我只想在所有测试之前初始化一次值 运行s
现在值 insertDbScheme
在每个测试中创建方案,如果可能的话 运行 它只创建一次。从 sbt 任务插入方案不合适
我有两个测试
测试 1:
class Test1 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
测试 2:
class Test2 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
和一个将方案插入数据库的基本特征
trait InsertDbScheme {
lazy val insertDbScheme = {
println("insert scheme to database")
}
}
运行宁测试的结果
insert scheme to database
insert scheme to database
[info] Test1
[info]
[info] + test
[info]
[info]
[info] Total for specification Test1
[info] Finished in 152 ms
[info] 1 example, 0 failure, 0 error
[info]
[info] Test2
[info]
[info] + test
[info]
[info]
[info] Total for specification Test2
[info] Finished in 152 ms
[info] 1 example, 0 failure, 0 error
[info]
[info] ScalaCheck
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] Passed: Total 2, Failed 0, Errors 0, Passed 2
[success] Total time: 12 s, completed May 18, 2017 6:35:21 PM
您可以使用 object
来确保它的 lazy val
只被实例化一次:
trait InsertDbScheme {
import InsertDbScheme._
lazy val insertDbScheme = insertDbSchemeOnce
}
object InsertDbScheme {
lazy val insertDbSchemeOnce = {
println("insert scheme to database")
}
}
最简单的方法是在 InsertDbScheme
对象中使用变量:
trait InsertDbScheme {
def insertDbScheme = synchronized {
if (!InsertDbScheme.done) {
println("insert scheme to database")
InsertDbScheme.done = true
}
}
}
object InsertDbScheme {
var done = false
}