gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 21:37:08 25 4
gpt4 key购买 nike

我想在 SBT 中分享我的“测试”和“它”配置之间的辅助特征,但我还没有弄清楚如何。

这是一个最小的例子:

项目/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/57251252/

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