I'm on scala, and I have multiple test files for different classes (testsuites), each of them uses testcontainers (init from the same script).
When I launch all tests in the project, all tests failed (problem connection with database due to testContainers).
When I launch separately tests, all the tests success.
Is there a way to launch only one container for multiple test files (testsuites)? TestContainerForAll
seems to work only for tests in the same file.
Edit after @Matthias Berndt reply :
Here libs that I'm using :
- "org.scalatest" %% "scalatest" % "3.0.8"
- "com.dimafeng" %% "testcontainers-scala-scalatest" % "0.38.1"
- "com.dimafeng" %% "testcontainers-scala-postgresql" % "0.38.1"
And here my code
trait DAOTest extends ForAllTestContainer {
this: Suite =>
override val container: PostgreSQLContainer = PostgreSQLContainer()
container.container.withInitScript("extractData.sql")
container.start()
ConfigFactory.invalidateCaches()
System.setProperty("jdbc.url", container.jdbcUrl)
ConfigFactory.invalidateCaches()
}
One of the options is (in java terms) to make an abstract base class, declare a container as a static variable and extend that class in your tests. In this case container will be created only once when base class is loaded.