[scalatest 2.2.2] BeforeAndAfterAll executing multiple times in a spec suite

21 views
Skip to first unread message

sili...@gmail.com

unread,
Oct 4, 2016, 12:47:36 PM10/4/16
to scalatest-users
Hi, 

I'm trying to write a fixture with BeforeAndAfterAll that initiates a database resource for my tests. As you may guess I'd like to do it only once per suite.

Simplified sample code: 

This is the shared Trait that initializes the db:

trait RepositorySpec extends WordSpecLike with BeforeAndAfterAll with ScalaFutures {

implicit val patience = PatienceConfig(timeout = Span(15, Seconds))

lazy val h2Database = Database.forURL(s"jdbc:h2:mem:testdb;MODE=PostgreSQL;DB_CLOSE_DELAY=-1")

override def beforeAll: Unit = {
h2Database.run(DBIO.sequence(Seq(sqlu"CREATE DOMAIN timestamptz AS timestamp with timezone;", Tables.schema.create))).futureValue
}

override def afterAll(): Unit = {
h2Database.run(sqlu"DROP DATABASE testdb;").futureValue
}

}

This is the test that uses it

class MyRepositorySpec extends fixture.WordSpecLike with MustMatchers with RepositorySpec with ScalaFutures {


import scala.concurrent.ExecutionContext.Implicits.global

override type FixtureParam = MyRepository

override def withFixture(test: OneArgTest): Outcome = withFixture(test.toNoArgTest(new MyRepository(h2Database)))

"Loan Repository" should {

"Some test" in { myRepository =>
myRepository.
someMethod(1).futureValue must be(...)
}

"Some other test" in { myRepository =>
myRepository.someOtherMethod(2).futureValue must be(...)
    }
}
}


I am seeing the berforeAll attempted on every test run.  I'm guessing it's because OneInstancePerTest is mixed in somewhere?

I've attempted the suggestion in this discussion but the test classes defined in the `nestedSuites` aren't being picked up.  

What am I doing wrong?  

Thanks
-greg
Reply all
Reply to author
Forward
0 new messages