‹ jan0sch.de

Scala and SBT: Switch from IntegrationTest config to tagged tests.

2023-06-13

With the release of version 1.9.0 sbt is deprecating the IntegrationTest configuration. Read on to learn how to migrate to tagged tests instead.

While the recommended migration path is to create a subproject in which you pool all your integration tests, I respectfully disagree and suggest to use tagging to filter tests during test runs.

Luckily for us the munit test framework has good support for this.

We just need to define some tags that we are going to use, for example within src/test/whatever/TestTags.scala

object TestTags {
  val RequiresDatabase = new munit.Tag("RequiresDatabase")
}

If you have a base class for your tests then you could also define your tags there to avoid having to import the tags every time.

abstract class BaseSpec extends munit.FunSuite {
  val RequiresDatabase = new munit.Tag("RequiresDatabase")
  ...
}

Then we simply move all our test code and resource files from the old src/it folder structure over to src/test. Watch out for things having the same name. Afterwards every test needs to be tagged properly which is as simple as this:

test("some integration test".tag(RequiresDatabase)) { }

To run only tests tagged with a specific tag use the following sbt command:

sbt> testOnly -- --include-tags=RequiresDatabase

To not run tests tagged with a specifiy tag use something like this:

sbt> testOnly -- --exclude-tags=RequiresDatabase

You can also mix both directives, please see the munit documentation for details.

Now it is time to remove everything related to IntegrationTest from your build configuration and the src/it folders. Keep an eye out for possible occurences of Defaults.itSettings in the build configuration and remove them too. :-)

Keep in mind that you have to adjust your continuous integration configuration on your build server of choice.