This article is part of a series on Tests in general and on how to practice them:
- Tools overview
- Test Driven Development
- TDD: just do it!
- spec BDD
- phpspec: a quick tour
- Behavior Driven Development: story BDD
- Behat: a quick tour
In this article, we'll talk about Behavior Driven Development (BDD). There's no special skills required to read on, although some notions on although some notions on tests and Test Driven Development (TDD) would be a plus.
From TDD to BDD
Dan North, a fervent TDDist, felt that there was something missing about this practice:
- you don't know where to start (which test should be written first?)
- you don't know when to stop (should I test getters/setters?)
- you don't know what defines a failure (compilation error? typo?)
While being an adapt of the concept, he felt that there was missing some important guidelines, and that his tests didn't answered the right questions.
Around 2006, he wrote an article called introducing BDD, in which he explained his unease and how to fix it: instead of writing tests from a technical point of view he decided to write them from a business point of view.
From unit tests to specification
The first step to test the behavior of a class is to write a specification. Because xUnit frameworks were in vogue, developers had the habit of making a parallel between their Systems Under Test (SUT) and their Test Suites:
- suffix the SUT's name with
Testto name the Test Suite
- prefix the SUT's method names with
testto name the Test Suite's methods
In the Software world, a lot of problems can be solved by giving descriptive names. And this is what Dan North did: Test Suites became Specification. Why? Because it forces you to say "this method should do this" instead of writting "test method".
To be more precise, Dan Nort stated that "test method names should be sentences". Which means that test method names should be snake_cased, as underscores give more spaces to words (they're basically spaces).
And that's pretty much it!
Is that all?
Yes! Well, no. I mean... This is only the first part of BDD: it's called specBDD (guess why ;) ). I'll write about the second part (story BDD) in a future article.
BDD is just TDD, but tests become specifications and test methods are sentences which start with "it_should".
I hope you enjoyed this article. If you want to make any comment, you're welcome to do so on Twitter.