0

I'm new to Scala. Coming from Java, I'm use to group/bundle my test classes in (JUnit) suites, in a hierarchical matter (suites within suites).

I'm looking for an alternative to this in ScalaTest.

Eyal Roth
  • 3,895
  • 6
  • 34
  • 45

2 Answers2

2

Any Suite can contain nested Suites. These are returned from the nestedSuites lifecyle method. You can use the Suites class to do this:

http://doc.scalatest.org/2.2.4/index.html#org.scalatest.Suites

If you want to disable discovery of the nested suites, you can use the @DoNotDiscover annotation:

http://doc.scalatest.org/2.2.4/index.html#org.scalatest.DoNotDiscover

Bill Venners
  • 3,549
  • 20
  • 15
0

Both FunSpec and FreeSpec allow nesting as much as you want. Examples from http://www.scalatest.org/user_guide/selecting_a_style:

import org.scalatest.FunSpec

class SetSpec extends FunSpec {

  describe("A Set") {
    describe("when empty") {
      it("should have size 0") {
        assert(Set.empty.size == 0)
      }

      it("should produce NoSuchElementException when head is invoked") {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }
  }

  // just add more describe calls
}

and

import org.scalatest.FreeSpec

class SetSpec extends FreeSpec {

  "A Set" - {
    "when empty" - {
      "should have size 0" in {
        assert(Set.empty.size == 0)
      }

      "should produce NoSuchElementException when head is invoked" in {
        intercept[NoSuchElementException] {
          Set.empty.head
        }
      }
    }

    // add more - calls
  }

  // add more - calls
}
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • 1
    Alexey, I think the question is about having different specs, like `SetSpec` and `ListSpec` and being able to create a suite called `CollectionsSpec`. – Eric Dec 07 '15 at 23:09
  • @Eric Yes, but that's the only way to achieve nesting with JUnit (AFAIK). ScalaTest allows this approach as well. – Alexey Romanov Dec 08 '15 at 06:31