Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
490 views
in Technique[技术] by (71.8m points)

sbt - How to make "test" classes available in the "it" (integration test) configuration?

I would like to share a helper trait between my "test" and "it" configurations in SBT, but I have not figured out how.

Here is a minimal example:

project/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)
  }
}

then, in sbt, "test" works:

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

but "it:test" doesn't compile:

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you want to share code from Test configuration, it's probably better to create a custom test configuration from Test. See Custom test configuration.

Your project/Build.scala becomes:

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
        )
      )
}

Also rename src/it/ to src/fun/. Now fun:test works:

> 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...