I made a change in the blogger configuration to ease the later work when blogging. It is possible that older entries are not correctly formatted.

Wednesday, 28 April 2010

Behavior Driven Development (aka. BDD) using EasyB

One more term for "test driven development", behaviour driven development. The idea behind the change in term is to highlight, that the principle underlying behind this style of development is more a design methodology as a test methodology.

To illustrate the purpose, and methodology, I will present it here using easyb.

The main idea is to write the specification as text, and then to replace the text using groovy code. Easyb provides two types of tests: specifications and stories.

I first describe the specification by giving an example.

before "initialize the queue for each spec", {
  queue = new Queue()
}

it "should dequeue gives item just enqueued", {
  queue.enqueue(2)
  queue.dequeue().shouldBe(2)
}
it "should throw an exception when null is enqueued", {
  ensureThrows(RuntimeException){
   queue.enqueue(null)
  }
}

easyb uses a number of keywords and methods. First of all, the keyword before for the code to be used when setting the test environment at the beginning. In this case a queue is created. The next important keyword is "it" at the beginning of certain lines. It indicates that this is a specification. It is followed by a text describing the specification. Finally, after the description some groovy code is included between "{" and "}".

Different methods belong to the easyb specification language: "shouldBe" and "ensureThrows". The first one checks that the result of the emthod to which it is applied returns a given value, whereas the second one verifyies that an exception as been thrown.

Maven Configuration

As usual, I find very useful to have a template of what I need in a maven pom to use somekind of framework. I haven't tested that yet. I still find that useful:

<project>
  ...
  <repositories>
   <repository>
     <id>easyb</id>
     <url>http://www.easyb.org/maven2/</url>
    </repository>
   </repositories>
   <pluginRepositories>
    <pluginRepository>
     <id>easyb</id>
     <url>http://www.easyb.org/maven2/</url>
    </pluginRepository>
   </pluginRepositories>
   ...
  </project>

Easyb clone for javascript

I also found the following code to perform bdd in javascript: JavaScript BDD framework in less than 200 lines.