仅当测试在依赖项目中传递时,才能在多项目sbt项目中运行测试

Cha*_*ton 11 testing scala sbt

假设一个带有foo项目和bar项目的多项目SBT项目,这样foo项目依赖于bar-project代码等.

如果bar-project中的测试通过,我想在foo-project中进行测试.

怎么样?

Ezh*_*hik 6

您可以在项目之间提供显式依赖关系.例如root - > A - > B.

GitHub上的测试用例.项目定义:

val commonSettings = Seq(libraryDependencies += "org.scalatest" %% "scalatest" % "1.9.1")

lazy val a: Project = (project in file("a")) settings(commonSettings: _*) settings(
    name := "a",
    test in Test <<= test in Test dependsOn (test in Test in b)
)

lazy val b: Project = (project in file("b")) settings(commonSettings: _*) settings(
    name := "b"
)

lazy val root: Project = (project in file(".")) settings(commonSettings: _*) settings(
    name := "root",
    test in Test <<= test in Test dependsOn (test in Test in a)
)
Run Code Online (Sandbox Code Playgroud)

从B开始并成功完成:

ezh@mobile ZZZZZZ % sbt-0.13                                          
[info] Set current project to root (in build file:/home/ezh/ZZZZZZ/)
> root/test
[info] Compiling 1 Scala source to /home/ezh/ZZZZZZ/b/target/scala-2.10/test-classes...
[info] TestSpecB:
[info] This test 
[info] - should fail
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info] Compiling 1 Scala source to /home/ezh/ZZZZZZ/a/target/scala-2.10/test-classes...
[info] TestSpecA:
[info] This test 
[info] - should succeed
[info] Passed: Total 1, Failed 0, Errors 0, Passed 1
[info] Passed: Total 0, Failed 0, Errors 0, Passed 0
[info] No tests to run for root/test:test
[success] Total time: 5 s, completed 28.11.2013 16:20:12
Run Code Online (Sandbox Code Playgroud)

从B开始但是失败:

ezh@mobile ZZZZZZ % sbt-0.13                                          
[info] Set current project to root (in build file:/home/ezh/ZZZZZZ/)
> test
[info] Compiling 1 Scala source to /home/ezh/ZZZZZZ/b/target/scala-2.10/test-classes...
[info] TestSpecB:
[info] This test 
[info] - should fail *** FAILED ***
[info]   2 did not equal 3 (Test.scala:5)
[error] Failed: Total 1, Failed 1, Errors 0, Passed 0
[error] Failed tests:
[error]         TestSpecB
[error] (b/test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 3 s, completed 28.11.2013 16:20:35
> 
Run Code Online (Sandbox Code Playgroud)