gpt4 book ai didi

sbt - 如何在 “test”(集成测试)配置中使 “it”类可用?

转载 作者:行者123 更新时间:2023-12-03 08:41:16 28 4
gpt4 key购买 nike

我想在SBT的“测试”和“it”配置之间共享一个辅助特性,但是我还没有弄清楚该如何做。

这是一个最小的示例:

项目/Build.scala

import sbt._
import Keys._

object MyBuild extends Build {

val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test,it"

lazy val myProject =
Project(id = "my-project", base = file("."))
.configs(IntegrationTest)
.settings(Defaults.itSettings: _*)
.settings(
scalaVersion := "2.10.3",
libraryDependencies ++= Seq(
scalaTest
)
)
}

src / test / scala / Helpers.scala
trait Helper {
def help() { println("helping.") }
}

src / test / scala / TestSuite.scala
import org.scalatest._

class TestSuite extends FlatSpec with Matchers with Helper {
"My code" should "work" in {
help()
true should be(true)
}
}

src / it / scala / ItSuite.scala
import org.scalatest._

class ItSuite extends FlatSpec with Matchers with Helper {
"My code" should "work" in {
help()
true should be(true)
}
}

然后,在sbt中,“测试”起作用:
sbt> test
helping.
[info] TestSuite:
[info] My code
[info] - should work
[info] Run completed in 223 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 0 s, completed Dec 17, 2013 1:54:56 AM

但是“it:test”无法编译:
sbt> it:test
[info] Compiling 1 Scala source to ./target/scala-2.10/it-classes...
[error] ./src/it/scala/ItSuite.scala:3: not found: type Helper
[error] class ItSuite extends FlatSpec with Matchers with Helper {
[error] ^
[error] ./src/it/scala/ItSuite.scala:5: not found: value help
[error] help()
[error] ^
[error] two errors found
[error] (it:compile) Compilation failed
[error] Total time: 1 s, completed Dec 17, 2013 1:55:00 AM

最佳答案

如果要共享Test配置中的代码,最好从Test创建自定义测试配置。参见Custom test configuration

您的project/Build.scala变为:

import sbt._
import Keys._

object MyBuild extends Build {
lazy val FunTest = config("fun") extend(Test)

val scalaTest = "org.scalatest" %% "scalatest" % "2.0" % "test"

lazy val myProject =
Project(id = "my-project", base = file("."))
.configs(FunTest)
.settings(inConfig(FunTest)(Defaults.testSettings) : _*)
.settings(
scalaVersion := "2.10.3",
libraryDependencies ++= Seq(
scalaTest
)
)
}

还要将 src/it/重命名为 src/fun/。现在 fun:test可以工作了:
> fun:test
helping.
[info] ItSuite:
[info] My code
[info] - should work
[info] Run completed in 245 milliseconds.
[info] Total number of tests run: 1
[info] Suites: completed 1, aborted 0
[info] Tests: succeeded 1, failed 0, canceled 0, ignored 0, pending 0
[info] All tests passed.
[success] Total time: 1 s, completed Dec 17, 2013 8:43:17 AM

关于sbt - 如何在 “test”(集成测试)配置中使 “it”类可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20624574/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com