Question

I have some questions regarding ScalaTest:

How can I ensure test-execution ORDER with ScalaTest, can I continue to use Junit4's @Suite.SuiteClasses annotation or is there another way without using Junit at all?

@RunWith(Suite.class)
@Suite.SuiteClasses({
    FirstTest.class, SecondTest.class
  })
public class MyTestSuite {
  @BeforeClass
  public void setup() {
    public MyDB db = new DB("config");
  }
}

Can I just translate the above to Scala and I am ready to go?

Another question is:
How can I make the "db" field available to the Suite-Classes? In Java the field would be static so I could just reference it from all Suite-Classes.
Maybe define a companion object which holds a reference?

Was it helpful?

Solution

In ScalaTest a suite can contain zero to many tests and zero to many nested suites. A suite's nested suites are provided by its nestedSuites method, and the names of the tests to run are provided by its testNames method. The general answer is that for any built-in ScalaTest style trait, the order of nested suite and test execution is determined by the order of they show up in the return value of nestedSuites and testNames. So if you're not otherwise getting the order you want, then you can override either or both of those methods.

However, for test order it is probably much easier to simply use one of the traits in which tests are functions, because those traits run tests in the order they appear in the source. (By contrast, a Suite runs tests in alphabetical order by test name.) So I'd suggest you use a FunSuite for starters. For example:

import org.scalatest.FunSuite

class MySuite extends FunSuite {
  test("this one will run first because it appears first") {
    // ...
  }
  test("this one will run second because it appears second") {
    // ...
  }
  test("this one will run third, and so on") {
    // ...
   }
 }

As far as getting suites to run in order, that's more rarely done because people like to use discovery to find new suites as they are written. But the way to do it is override nestedSuites. In 1.5 there the Suites class is a convenient way to do that:

import org.scalatest.Suites

class MySuites extends Suites(
  new MyFirstSuite,
  new MySecondSuite,
  new MyThirdSuite,
  new MyFourthSuite
)

Then you run MySuites and it will run its nested suites in the order you declared them.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top