Search code examples
scalasbtscalatest

How can I execute a single nested ScalaTest Suite?


In ScalaTest, it is possible to nest Suites by overriding the nestedSuites method. This is useful when an expensive to initialize resource, such as a container from the testcontainers library, is needed in several different suites that are otherwise unrelated. One can then initialize the resource in the outer Suite's beforeAll method, pass it to the nested Suites as a constructor parameter, and clean it up in the afterAll method.

However there's a downside: it's not possible any more to launch the inner Suites with sbt's testOnly command. How can I run these inner Suites individually?


Solution

  • While sbt's internals do contain provisions for this in the form of the NestedSuiteSelector class, this is not exposed through the testOnly command. The only solution for now is therefore to run the ScalaTest Runner and pass appropriate command line arguments: test:runMain org.scalatest.tools.Runner -s my.project.OuterSuite -i my.project.InnerSuite -o

    More details can be found in the documentation: http://doc.scalatest.org/3.1.1/org/scalatest/tools/Runner$.html