如何在“it”(集成测试)配置中提供“测试”类?

时间:2013-12-17 01:58:04

标签: sbt

我想在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 /测试/阶/ Helpers.scala

trait Helper {
  def help() { println("helping.") }
}

的src /测试/阶/ TestSuite.scala

import org.scalatest._

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

的src /它/阶/ ItSuite.scala

import org.scalatest._

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

然后,在sbt中,“test”起作用:

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

但是“它: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

2 个答案:

答案 0 :(得分:17)

如果您想共享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

答案 1 :(得分:14)

您可以在项目中重新定义IntegrationTest Configuration以扩展Test配置而不是Runtime Configuration(默认值)。这将使您的测试配置中的所有内容都可用于IntegrationTest配置。

import sbt._
import Keys._

object MyBuild extends Build {

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

  lazy val IntegrationTest = config("it") extend(Test)

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