I want to initializate value only once before all tests runs
Now the value insertDbScheme
create scheme in every test, if it possible to run it only once. Insert scheme from sbt task not suitable
I have two tests
test1:
class Test1 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
test2:
class Test2 extends Specification with InsertDbScheme {
"test" in {
insertDbScheme
// some code
ok
}
}
and a base trait that insert scheme to database
trait InsertDbScheme {
lazy val insertDbScheme = {
println("insert scheme to database")
}
}
result of running tests
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
You can use an object
to make sure it's lazy val
is only instantiated once:
trait InsertDbScheme {
import InsertDbScheme._
lazy val insertDbScheme = insertDbSchemeOnce
}
object InsertDbScheme {
lazy val insertDbSchemeOnce = {
println("insert scheme to database")
}
}