Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 在Playframework启动之前启动testcontainers_Scala_Playframework_Scalatest_Testcontainers - Fatal编程技术网

Scala 在Playframework启动之前启动testcontainers

Scala 在Playframework启动之前启动testcontainers,scala,playframework,scalatest,testcontainers,Scala,Playframework,Scalatest,Testcontainers,我想在play应用程序(使用slick)启动之前,从docker compose文件(postgres和kafka实例)启动testcontainers。我想要这个,这样我就可以写一个端到端的测试。我似乎不明白这是怎么可能的发挥 import java.io.File import com.dimafeng.testcontainers.DockerComposeContainer.ComposeFile import com.dimafeng.testcontainers.{DockerCom

我想在play应用程序(使用slick)启动之前,从docker compose文件(postgres和kafka实例)启动testcontainers。我想要这个,这样我就可以写一个端到端的测试。我似乎不明白这是怎么可能的发挥

import java.io.File
import com.dimafeng.testcontainers.DockerComposeContainer.ComposeFile
import com.dimafeng.testcontainers.{DockerComposeContainer, ForAllTestContainer}
import com.typesafe.config.ConfigFactory
import org.scalatest.{BeforeAndAfterAll, FunSpec}
import org.scalatestplus.play.guice.GuiceFakeApplicationFactory
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.{Application, Configuration, Environment, Mode}

trait TestFunSpec extends FunSpec with BeforeAndAfterAll with GuiceFakeApplicationFactory {

  override def fakeApplication(): Application = new GuiceApplicationBuilder()
    .in(Environment(new File("."), getClass.getClassLoader, Mode.Test))
    .loadConfig(_ => Configuration(ConfigFactory.load("test.conf")))
    .build

}

class TestIntegrationSpec extends TestFunSpec with ForAllTestContainer {

  override val container = DockerComposeContainer(ComposeFile(Left(new File("docker-compose.yml"))))

  it("should test something") {

    assert(true)

  }
}
Scala版本2.12.10 Testcontainer版本0.35.0 播放slick 5.0.0版

当我在没有“TestFunSpec”的情况下执行测试时,docker compose会正确地启动我的服务。当我在作用域中添加播放应用程序“TestFunSpec”时,应用程序会尝试启动,在启动时,它会尝试连接到postgres,而postgres还不存在(因为testcontainers随后会启动)

提前通知我


更新:有关详细的答案,请参见答案部分。

在深入研究游戏测试套件机制后,我想出了一个可行的设置

步骤1,使用AppProvider定义测试容器套件:

import com.dimafeng.testcontainers.{Container, ForAllTestContainer}
import org.scalatest.Suite
import org.scalatestplus.play.AppProvider

trait PlayTestContainer extends Suite with AppProvider with ForAllTestContainer {

  override val container: Container

}
步骤2,创建一个抽象的PlayTestContainerIntegrationSpec,它扩展了上述特性:

import java.io.File
import com.typesafe.config.ConfigFactory
import org.scalatest.concurrent.{IntegrationPatience, ScalaFutures}
import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, TestData}
import org.scalatestplus.play.PlaySpec
import org.scalatestplus.play.guice.GuiceOneAppPerTest
import play.api.inject.guice.GuiceApplicationBuilder
import play.api.{Application, Configuration, Environment, Mode}

abstract class PlayTestContainerIntegrationSpec
    extends PlaySpec
    with PlayTestContainer
    with GuiceOneAppPerTest
    with ScalaFutures
    with IntegrationPatience
    with BeforeAndAfterEach
    with BeforeAndAfterAll {

  override def newAppForTest(testData: TestData): Application = application()

  def application(): Application =
    new GuiceApplicationBuilder()
      .in(Environment(new File("."), getClass.getClassLoader, Mode.Test))
      .loadConfig(_ => Configuration(ConfigFactory.load("test.conf")))
      .build
}
如您所见,我们包含了“PlayTestContainer”特性,并重写了“newAppForTest”函数来构建播放应用程序

步骤3,创建特定集成测试,扩展上述抽象PlayTestContainerIntegrationSpec,并覆盖容器,以满足您的特定需要:

class TestIntegrationSpec extends PlayTestContainerIntegrationSpec {

  override val container = DockerComposeContainer(ComposeFile(Left(new File("docker-compose.yml"))))

  "should test something" in {

    assert(true === true)

  }
}

希望这有帮助。

您想在所有测试用例之前还是在每个测试用例之前启动它?在启动播放应用程序之前。所以,在初始化所有测试用例之前。