scala SBT测试不运行zio测试,尽管包含测试框架ZTestFramework

k4emjkb1  于 2023-08-05  发布在  Scala
关注(0)|答案(1)|浏览(141)

我有一个Scala 2.3.11,sbt 1.9.3,zio 2.0.15,zio-test 2.0.15的项目。
在这个项目中,我混合了scalatest测试org.scalatest.flatspec.AnyFlatSpec和ZIO Junit测试zio.test.junit.JUnitRunnableSpec
我加入了这行:

testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")

字符串
以及以下依赖项:

val zioVersion = "2.0.15"
  val scalaTestVersion = "3.2.16"

  ...

  val testDeps = Seq(
    "org.scalactic" %% "scalactic" % scalaTestVersion,
    "org.scalatest" %% "scalatest" % scalaTestVersion % Test,
    "dev.zio" %% "zio-test" % zioVersion % Test,
    "dev.zio" %% "zio-test-sbt" % zioVersion % Test,
    "dev.zio" %% "zio-test-magnolia" % zioVersion % Test,
    "dev.zio" %% "zio-test-junit" % zioVersion % Test,
  )


在SBT中,如果我尝试:

sbt:core> show testFrameworks
[info] * TestFramework(org.scalacheck.ScalaCheckFramework)
[info] * TestFramework(org.specs2.runner.Specs2Framework, org.specs2.runner.SpecsFramework)
[info] * TestFramework(org.specs.runner.SpecsFramework)
[info] * TestFramework(org.scalatest.tools.Framework, org.scalatest.tools.ScalaTestFramework)
[info] * TestFramework(com.novocode.junit.JUnitFramework)
[info] * TestFramework(munit.Framework)
[info] * TestFramework(zio.test.sbt.ZTestFramework)
[info] * TestFramework(weaver.framework.CatsEffect)
[info] * TestFramework(hedgehog.sbt.Framework)


因此,ZTestFramework已正确添加。
我可以在Intellij中轻松运行ZIO Junit测试。
但是,当我运行sbt test时,我期望运行ZIO测试。相反,只有scalatest测试运行。
知道为什么它不接收我的测试吗有没有办法进一步调试ZTestFramework

fcg9iug3

fcg9iug31#

通过创建以下文件,我能够使用命令sbt test运行所有测试。

  • build.sbt
libraryDependencies ++= Seq(
  "org.scalactic"     %% "scalactic"         % "3.2.16",
  "org.scalatest"     %% "scalatest"         % "3.2.16"   % Test,
  "org.scalatestplus" %% "scalacheck-1-17"   % "3.2.16.0" % Test,
  "dev.zio"           %% "zio"               % zioVersion,
  "dev.zio"           %% "zio-test"          % zioVersion % Test,
  "dev.zio"           %% "zio-test-sbt"      % zioVersion % Test,
  "dev.zio"           %% "zio-test-magnolia" % zioVersion % Test,
  "dev.zio"           %% "zio-test-junit"    % zioVersion % Test
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")

字符串

  • ZioHelloWorld.scala
import zio._
import java.io.IOException

object ZioHelloWorld {
  def sayHello: ZIO[Any, IOException, Unit] =
    Console.printLine("Hello, World!")
}

  • ZioHelloWorldSpec.scala
import ZioHelloWorld._
import zio.test._

object ZioHelloWorldSpec extends ZIOSpecDefault {
  def spec =
    suite("ZioHelloWorldSpec")(test("sayHello correctly displays output zio test") {
      for {
        _ <- sayHello
        output <- TestConsole.output
      } yield assertTrue(output == Vector("Hello, World!\n"))
    })
}

  • ZioJunitHelloWorldSpec.scala

你有两种Integrating ZIO Test with JUnit的方法。一个是下面添加的示例,另一个是在类定义上方添加注解@RunWith(classOf[ZTestJUnitRunner]),混合ZIOSpecDefault而不是JUnitRunnableSpec,并添加库"com.github.sbt" % "junit-interface" % "0.13.3" % Test

import ZioHelloWorld._
import zio.test._
import zio.test.junit.JUnitRunnableSpec

object ZioJunitHelloWorldSpec extends JUnitRunnableSpec {
  def spec =
    suite("ZioJunitHelloWorldSpec")(test("sayHello correctly displays output using junit") {
      for {
        _ <- sayHello
        output <- TestConsole.output
      } yield assertTrue(output == Vector("Hello, World!\n"))
    })
}

  • HelloScalaTest.scala
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks

class HelloScalaTest extends AnyFunSuite with Matchers with ScalaCheckPropertyChecks {

  test("1 should be 1") {
    1 should be(1)
  }

  test("(a + b) should be (b + a)") {
    forAll { (a: Int, b: Int) =>
      (a + b) should be(b + a)
    }
  }

}


执行sbt test后,我得到以下输出

+ ZioHelloWorldSpec
Hello, World!
Hello, World!
  + sayHello correctly displays output zio test
+ ZioJunitHelloWorldSpec
  + sayHello correctly displays output using junit
[info] HelloScalaTest:
[info] - 1 should be 1
[info] - (a + b) should be (b + a)
4 tests passed. 0 tests failed. 0 tests ignored.

相关问题