Tagging ScalaTest Tests and SBT

I want to share a trick for a combination I suspect a number of people are using: SBT and ScalaTest

When you’re writing tests, you may have occasion to segregate some tests from the others – perhaps your functional tests, integration tests, or ui tests, or just tests that run more slowly than the rest of your suite.

There are a number of ways to tackle this (one good way is with SBT sub-projects, which I’ll cover in another post), but another option is to “tag” your tests. The ScalaTest doc covers tagging here, but what’s not as clear is how you do this and still run your tests with SBT.

Let’s say you have tagged some tests with the tag “Ui”, so you’ve got test declarations that look like this:

<br /> describe("some thing I want to test") {<br /> it ("should do a thing", Ui) {<br /> ....<br /> }<br /> }<br />

I only want to run this test (and all other tests tagged “Ui”) in one particular job on my CI server.

What I need to do is pass the -l and -n options to ScalaTest, but I want to do this only when a certain system property is passed to my SBT instance. E.g. if I have a property “ui”, I can pass it to SBT with “-Dui=true”. How do I get this property to set the right option for ScalaTest so that when ui is true, ONLY my Ui-tagged tests run, and when ui is false, all the other tests EXCEPT the Ui-tagged tests run?

Here’s what I did in my build.sbt file:

<br /> testOptions in Test ++= (if (System.getProperty("ui", "false") == "false") Seq(Tests.Argument("-oDF"), Tests.Argument("-l"), Tests.Argument("Ui")) else Seq(Tests.Argument("-oDF"), Tests.Argument("-n"), Tests.Argument("Ui")))<br />

What this says is that I always want the arguments “-oDF” passed to Scalatest, but that when ui is false, I want “-l Ui” and when ui is true, I want “-n Ui”. I default ui to false, so if it’s not specified at all, thats the same as false.

Now I can simply set a system property for my Jenkins builds, and the right tests are run at the right time.

I thought this might be helpful to others, as it took a bit of digging to figure out the exact syntax.

Note that this appears only to work with Scalatest 1.7RC1 (or later, probably), and SBT 0.11.2 or later.

Published: January 29 2012