【问题标题】:How to make "test" classes available in the "it" (integration test) configuration?如何使“测试”类在“it”(集成测试)配置中可用?
【发布时间】:2014-01-04 15:35:35
【问题描述】:

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

这是一个最小的例子:

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

然后,在 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

【问题讨论】:

    标签: sbt


    【解决方案1】:

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

    【讨论】:

    • @Eugene Yokota 在使用这种自定义配置时是否只运行特定的TestSuite(测试类)?尝试fun:testOnly *MySpecialSuit 没有运气
    【解决方案2】:

    您可以在项目中重新定义 IntegrationTest 配置以扩展测试配置而不是运行时配置(默认)。这将使您的测试配置中的所有内容都可用于您的 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
            )
          )
    }
    

    【讨论】:

    • 如果我这样做了,如果构建配置的其他部分看到阴影的“真实”IntegrationTest,难道不会有奇怪的陷阱潜伏吗?
    • 如果你将两者都拉到其他地方,它可能无法编译,但即使你明确选择了错误的,它也应该在内部解析到相同的配置空间,因为两个配置都绑定到“它”,我认为这才是真正重要的。 (如果愿意,您可以随时将其重命名为 MyIntegrationTest。)
    猜你喜欢
    • 2019-12-06
    • 2012-12-12
    • 1970-01-01
    • 2014-02-02
    • 1970-01-01
    • 2016-09-25
    • 2015-06-14
    • 2015-10-11
    • 1970-01-01
    相关资源
    最近更新 更多