Fork me on GitHub

Pease

Pease acceptance testing spock extension

Introduction

Pease is a compiler that creates Spock tests from Gherkin specifications. With Pease you are able to separate your requirements and your test code and still access the full power of the Spock framework.

Since Pease uses the well known Gherkin DSL, that is also used by Cucumber, it enables your whole team to participate in your software development process. You can use Pease to do Acceptance Test-driven development (ATDD) on the JVM.

Specification in Gherkin a business readable DSL

Feature: Addition
  Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen

Step definitions in Groovy with Spock code inside the closures

Before {
  def calc = new Calculator()
}

Given(~/I have entered (.*) into the calculator/) { n ->
  calc.push(n as double)
}

When(~/I press add/) {
  calc.add()
}

Then(~/the result should be (.*) on the screen/) { n ->
  calc.result == n as double
}

Generated spock test either you get this or a compiled JUnit test class

This code is the result of the compilation process. Normally you do not see this test and

class Addition extends spock.lang.Specification {
    void "add two numbers"() {
    def calc = new Calculator()

    given: "I have entered 50 into the calculator"
    calc.push("50" as double)

    and: "I have entered 70 into the calculator"
    calc.push("70" as double)

    when: "I press add"
    calc.add()

    then: "the result should be 120 on the screen"
    calc.result == "120" as double
  }
}

How does it work? technical details (for those who care)

Here is a code snippet that demonstrates the behavior of Pease while generating the above Spock test. In a real world scenario you will not have to deal with this kind of technical details. This demonstration is here to show you what kind of magic is involved.

Feel free to play around with this example using your local groovyConsole (the Groovy web console won't work, because it has no Grape support). All dependencies are automatically resolved using Grape. Just copy and paste the script into your groovyConsole and execute it.

Things you could try:

  • Change the feature file by modifying the featureFile variable.
  • Adjust the regular expressions of the step definitions by editing the stepDefinitionTree variable.
  • Try what happens when a step without matching step definition is present.
@Grapes([
  @GrabResolver(name="pease", root="http://pease.github.com/maven/"),
  @GrabResolver(name="cukes", root="http://cukes.info/maven/"),
  @Grab("pease:pease:0.1.0")
])

import pease.gherkin.FeatureLoader
import pease.groovy.StepLoader
import pease.Configuration

def featureFile = '''
Feature: Addition
  Scenario: Add two numbers
    Given I have entered 50 into the calculator
    And I have entered 70 into the calculator
    When I press add
    Then the result should be 120 on the screen
'''

def stepDefinitionTree = StepLoader.instance.loadFromString('''
  Before {                                                  // line 2
    def stack = []
  }

  Given(~/I have entered (.*) into the calculator/) { n ->  // line 6
    stack << (n as double)
  }          

  When(~/I press add/) {                                    // line 10
    def result = stack.pop() + stack.pop()
  }

  Then(~/the result should be (.*) on the screen/) { n ->   // line 14
    result == (n as double)
  }
''')
def config = new Configuration()

FeatureLoader.instance.loadFromString(featureFile) \
    >>> { it.compileSpock(stepDefinitionTree, config) } \
    >>> { println it }