Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make tests in multi project sbt projects run only if tests pass in dependent projects

Tags:

testing

scala

sbt

Assuming a multiproject SBT project with a foo-project and bar-project, such that foo-project depends on bar-project for code etc.

I would like tests in foo-project to run iff the tests in bar-project pass.

How?

like image 502
Channing Walton Avatar asked Nov 22 '13 15:11

Channing Walton


People also ask

What is a subproject in sbt build?

Each subproject in a build has its own source directories, generates its own jar file when you run package, and in general works like any other project. A project is defined by declaring a lazy val of type Project. For example, : The name of the val is used as the subproject’s ID, which is used to refer to the subproject at the sbt shell.

How to aggregate multiple projects in SBT?

Start up sbt with two subprojects as in the example, and try compile. You should see that all three projects are compiled. In the project doing the aggregating, the root project in this case, you can control aggregation per-task. For example, to avoid aggregating the update task:

How do I find the current project in SBT?

The usual sbt directory structure applies underneath foo with the exception of build definition files. At the sbt interactive prompt, type projects to list your projects and project <projectname> to select a current project. When you run a task like compile, it runs on the current project.

How to access resources from tests in SBT?

The resources may be accessed from tests by using the getResource methods of java.lang.Class or java.lang.ClassLoader . The main Scala testing frameworks ( ScalaCheck , ScalaTest, and specs2) provide an implementation of the common test interface and only need to be added to the classpath to work with sbt.


1 Answers

You may provide explicit dependencies between projects. For example root -> A -> B

Test case on GitHub. Project definition:

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

Beginning from B and complete them successful:

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

Beginning from B but fail:

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
> 
like image 104
Ezhik Avatar answered Nov 16 '22 01:11

Ezhik