sbt tests and multiple modules
A test suite under src/test/scala needs no command of its own: sbt test compiles the test scope and runs every suite it finds. Once a program grows past one purpose, the build grows a module per purpose, and dependsOn and aggregate decide what sees what and what runs when the top-level build is asked to do something.
Two modules and a root
core holds the code with no dependencies, app depends on it and holds the report and the suite, and root aggregates both. ThisBuild / ... sets the Scala version and the organization once for every module:
build.sbt
ThisBuild / scalaVersion := "3.10.1"
ThisBuild / organization := "demo"
lazy val core = (project in file("core"))
.settings(name := "core")
lazy val app = (project in file("app"))
.dependsOn(core)
.settings(
name := "app",
libraryDependencies += "org.scalameta" %% "munit" % "1.3.6" % Test
)
lazy val root = (project in file("."))
.aggregate(core, app)
.settings(name := "word-tools")
core/src/main/scala/Words.scala
object Words:
def split(text: String): List[String] =
text.toLowerCase.split("\\W+").toList.filter(_.nonEmpty)
app/src/main/scala/Report.scala
object Report:
def line(text: String): String =
Words.split(text).mkString(" ")
app/src/test/scala/ReportSuite.test.scala
//> using test.dep org.scalameta::munit::1.3.6
class ReportSuite extends munit.FunSuite:
test("a sentence becomes its words") {
assertEquals(Report.line("Scala, and sbt!"), "scala and sbt")
}
test("an empty text becomes an empty line") {
assertEquals(Words.split(" "), Nil)
}
Report reads Words only because app declares dependsOn(core): without that line the compiler would not find the object, and the suite would not compile. The directive at the top of the suite is a comment to sbt and a dependency declaration to scala-cli; the same MUnit 1.3.6 is declared in build.sbt with % Test, which is what the build reads. One suite, run from the top:
sbt --server --batch "clean;test"
Test run ReportSuite started
ReportSuite: finished <TIME>
Test run ReportSuite finished: 0 failed, 0 ignored, 2 total <TIME>
[info] passed: total 1, failed 0, errors 0, passed 1, cached 0
[info] passed: total 1, failed 0, errors 0, passed 1, cached 0
The suite is in app and it reads both modules, which is the proof that the aggregate and the dependency both work. The two passed lines are the aggregate: root asks both modules to test, and core has no suite, so its line reports the same single suite the app line does. clean; is there because sbt caches a test result whose inputs did not change, and a page that shows the report asks for a fresh run.
aggregate and dependsOn answer different questions. aggregate says which modules a task of the root runs; dependsOn says which module's classes are on the compile class path. A build usually has both: the root aggregates everything so that one sbt test is the whole check, and a module depends only on what it imports.
Related
- The sbt project layout: the single module this build starts from.
- build.sbt, settings and tasks: the file the modules are declared in.
- MUnit: the suite the build runs.
- Packaging: what one module of a build becomes when it ships.
Автор статьи: Arthur Isaev