Auto-numbered examples

In the named examples section we can see that there is a way to create groups of examples with standard names and use these names in the specification text (via the s2 interpolated string). Here we go a step further.

The specification text is going to be some pure text, the examples will be described as example groups and the numbers will be created automatically. What does it look like?

class BulletedExamplesSpec extends script.Specification with Groups { def is = """
  This is an introduction

  First section

  A bit more text there.
   + and the first example
   + and the second example

  Second section

  Then another section
   + and the third example
   + and the fourth example


  "first section" - new group {
    eg := 1 must_== 1
    eg := 1 must_== 1

  "second section" - new group {
    eg := 1 must_== 1

This style of specification uses the org.specs2.specification.script.Specification class and mixes-in the Groups trait to create example groups. You can notice that:

At runtime, the text gets parsed and:

So, in the specification above the fourth example will be marked as pending (g2.e2) where g2 is the group name and e2 is the example name. As you can guess, sections and tags make it easy to just re-run parts of the specification without having to create those sections and tags yourself.

The major issue with this style of specification is that you cannot use your IDE to navigate from a piece of text to the corresponding code. You have to count the groups and count the number of examples to find the right one.