Search code examples
scalaunit-testingakkaactorscalatest

How to tag tests in akka's TestKit using scala?


I want to tag certain tests in order to (not) run them in certain scenarios. (E.g. slow that should only run during build, yet not while developing.)

In Scala using ScalaTest's FlatSpec I know I can use taggedAs and scalatest's Tag.

Yet I don't know how to achieve the same thing within an actor test. I have have TestCase that extends TestKit and there seems to be no taggedAs functionality in there as FlatSpec provides.

I want to be able to filter in/out the tests via

sbt "testOnly -- -l myTag" # runs all except the tagged tests
sbt "testOnly -- -n myTag" # only runs the tagged tests

How can I achive that with akka's test framework?


Solution

  • Akka's TestKit assumes the use of a testing framework. From the documentation:

    Akka’s own test suite is written using ScalaTest, which also shines through in documentation examples. However, the TestKit and its facilities do not depend on that framework, you can essentially use whichever suits your development style best.

    In other words, one uses TestKit in conjunction with a framework like ScalaTest. Mix in TestKit and whatever ScalaTest traits you want to use (e.g., FlatSpec) into your test, and you can use taggedAs.

    For example, consider one of Akka's internal tests, SupervisorHierarchySpec, that uses TestKit with ScalaTest tagging:

    class SupervisorHierarchySpec extends AkkaSpec(SupervisorHierarchySpec.config) with DefaultTimeout with ImplicitSender {
      ...
      "A Supervisor Hierarchy" must {
    
        "restart manager and workers in AllForOne" taggedAs LongRunningTest in {
           ...
        }
        ...
      }
    }
    

    This test extends AkkaSpec, which is an abstract class that mixes in both TestKit and ScalaTest traits:

    abstract class AkkaSpec(_system: ActorSystem)
      extends TestKit(_system) with WordSpecLike with Matchers with BeforeAndAfterAll with WatchedByCoroner
      with ConversionCheckedTripleEquals with ScalaFutures {
      ...
    }
    

    And LongRunningTest is a ScalaTest tag.