9 05 2007

BDD - Behaviour Driven Development.

I once was a fan of TDD (aka. Test Driven Development) … whereas I use it in languages where it just naturally fit (e.g. Ruby, Perl) or in language where it’s less obvious (think C)… One of the neat effects of this way of coding IMHO, is that it forces you to act as a user/client of the class/API you’re developping, and thus to get immediately feedback on the usuability/coolness of your class.

In that sense, BDD is not far from TDD … with a more positive attitude. With TDD, you are testing your code, with all the negativeness carried by the word “testing”. With BDD, you’re specifying what your code is supposed to do, you write that code, and you check the code you just wrote match the specifications.

Moreover, depending on the framework you’re using, this can be fun to read and write. For example, using RSpec, for Ruby:

Code (ruby)

context "A username with a strange-case fullname" do
  setup do
    @username = "joobarbaz", "jean-Christophe oobar BAZ" )

  specify "should have correctly capitalized firstname" do
    @username.firstname.should == "Jean-Christophe"

  specify "should have correctly capitalized lastname" do
    @username.lastname.should == "Oobar Baz"

DSL defined by RSpec, allows to easily specify what your methods are supposed to do and to test it. Running the snippet above leads to something like:

$> spec -fs username-spec.rb
A username with a strange-case fullname
- should have correctly capitalized firstname
- should have correctly capitalized lastname

… which leads to specifications listing while testing these specifications.

You can thus write your class specifications, using the framework DSL, and then write the code that will match these specs: specifications and code will stay synchronized … we’re not so far from D. Knuth own “literate programming” ;)



Erreur de la base de données de WordPress : [Table 'sl33p3r.wp_comments' doesn't exist]
SELECT * FROM wp_comments WHERE comment_post_ID = '7' AND comment_approved = '1' ORDER BY comment_date

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>