Skip to main content

Useful tricks in easyb - tags and parallel tests

Posted by johnsmart on July 21, 2010 at 11:50 AM PDT

Easyb is an excellent BDD testing framework, with a heap of very cool features. In this article, we look at two more recent features: tags and parallel tests.

If you're looking for a good Behavior-Driven Development (BDD) or Acceptance-Test Driven Development (ATDD), you owe it to yourself to check out Easyb. Easyb is a great BDD-style testing framework where you express your tests as "stories" and "scenarios", using a highly-readable form based on the familiar "given-when-then" structure. A simple story (containing a single scenario) is shown here:

scenario "transfer funds to a savings account",{
given "a current account containing $200"
and "a savings account containing $1000"
when "when I transfer $100 from the current to the savings account"
then "the current account should contain $100"
and "the savings account should contain $1100"
}

One of the nice things about easyb is that this is actually a fully executable easyb story - put it in a file called (say) TransferFunds.story, and easyb will be happy to run it, include in the easyb reports, and flag it as 'pending implementation'.

When you are ready to actually implement this story, you add the appropriate code into each step. For example, here is an example of an acceptance-test from a web application implementing Conway's Game Of Life:

before_each "assume that the user has seeded the universe", {
    given "that the application is up and running", {
        homePage = new HomePage(new HtmlUnitDriver())        
    }
    and "that the user has opened the 'new game' page", {
        homePage.open();
        newGridPage = homePage.clickOnNewGameLink()
    }
}

scenario "Generating a new generation of cells",{
    given "that the user enters a stable set of living cells", {
        newGridPage.clickOnCellAt(0,0)
        newGridPage.clickOnCellAt(0,1)
        newGridPage.clickOnCellAt(1,0)
        newGridPage.clickOnCellAt(1,1)
        gridDisplayPage = newGridPage.clickOnGoButton()
    }
    when "the user clicks on 'Next Generation'", {
        gridDisplayPage = gridDisplayPage.clickOnNextGenerationButton()
    }
    then "the application will display a universe containing the same cells", {
        String[][] theInitialGrid = [["*", "*", "."],
                                     ["*", "*", "."],
                                     [".", ".", "."]]
        gridDisplayPage.displayedGrid.shouldBe theInitialGrid
    }
}

Easyb also supports tags, which are a very flexible way of categorizing your stories. For example, you can use tags to identify stories related to a particular feature, or to identify the stories being implemented in the current sprint. Or you can use tags to identify a story with the corresponding issue in your issue tracking system. If you identify all of the stories in the current sprint, for example, you can easily set up a build configuration to run, and report on, just the stories for this sprint. Of course your full build would run all of your stories, but this special sprint-specific build is a great reporting tool to let you know which stories have been completed and which are still pending implementation.

Easyb tags are easy to use. You can add a tag to a story as shown here:

tags "transfers"

scenario "transfer funds to a savings account",{
given "a current account containing $200"
and "a savings account containing $1000"
when "when I transfer $100 from the current to the savings account"
then "the current account should contain $100"
and "the savings account should contain $1100"
}

scenario "transfer funds to a current account",{
  ...
}

You can put as many tags as you want on a story - here's an example of a story with two tags:

tags ["transfers", "sprint-2"]

scenario "transfer funds to a savings account",{
given "a current account containing $200"
and "a savings account containing $1000"
when "when I transfer $100 from the current to the savings account"
then "the current account should contain $100"
and "the savings account should contain $1100"
}

In the current version of easyb, you can only apply tags to stories, not scenarios - scenario-based tagging is planned for a future version.

The latest version of the Easyb Maven plugin (version 0.9.7-1) provides full support for tags. For example, you could run only the 'sprint-2' stories using the following configuration:

<build> 
  <plugins>
    <plugin>
      <groupId>org.easyb</groupId>
      <artifactId>maven-easyb-plugin</artifactId>
      <version>0.9.7-1</version>
      <executions>
        <execution>
          <goals>
            <goal>test</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
          <tags>sprint-2</tags>
      </configuration>
    </plugin>
  </plugins>
</build>

Another very useful trick is parallel testing. I use easyb extensively for acceptance tests, involving web tests or other end-to-end tests. With lots of I/O and database interaction, these tests are perfect candidates for being run in parallel. If you add the parallel configuration option, as shown below, easyb will create up to ten threads and run your stories in parallel:

<build> 
  <plugins>
    <plugin>
      <groupId>org.easyb</groupId>
      <artifactId>maven-easyb-plugin</artifactId>
      <version>0.9.7-1</version>
      <executions>
        <execution>
          <goals>
            <goal>test</goal>
          </goals>
        </execution>
      </executions>
      <configuration>
        <tags>sprint-2</tags>
        <parallel>true</parallel>
      </configuration>
    </plugin>
  </plugins>
</build>

But wait! There's more! Easyb is a dynamic project with some very smart dudes working on it, and there are some very cool new features are in the pipeline, including data-driven tests and JUnit-compatible reports! Watch this space!

If you want to learn more about BDD and ATDD, be sure to check out the upcoming Java Power Tools Bootcamps, coming up soon in Wellington, London and Canberra.

Related Topics >>