BDD – How to Organize Unit/Integration Tests in BDD

bddcucumberpythontdd

So finally after reading a lot, I have understood that the difference between BDD and TDD is between T & B. But coming from basic TDD background, what I used to was,

  1. first write unittest for database models

  2. write test for views (at this point start with integration test as well, along with unittests)

  3. write more integration tests for testing UI stuff.

What would be a correct way to approach BDD. Say I have a simple blog application.

Given : When a user logs in. 
He should be shown list of all his posts. 

But for this, I need a model with a row user, another row blog posts.
So how do we go about writing tests?
when do we create fixtures? When do we write integration (selenium) tests?

Best Answer

The prerequisites are the "Given" part of the equation. This should preferably access the database directly and create the objects you need. So for your case:

Given user "whatf" exists
And user "whatf" has 3 blog posts
When "whatf" logs in
Then he should be shown a list of all his posts

Now, step user "whatf" exists creates a user in the database (we name the user so that we can have a specific definition of the user behind-the-scenes, such that you don't have to say user exists with admin rights and pink theme and password "s3cret" ... over and over).

Step user "whatf" has 3 blog posts creates three blog posts in the database, against that new user. You can be more or less specific here, if you choose. More specific gives you flexibility, less specific gives you more reusable steps.

Step "whatf" logs in should be the first time you use Selenium, to launch the browser and log the user in.

Then, in the confirmation step, you also use Selenium to check that the user is shown a list of all his posts in the response.

Also note that the difference between TDD and BDD is really dictionary. BDD encourages a more behaviour-driven language for your tests. But those tests can still be unit tests. Or they can be acceptance tests (ATDD).

But the difference between what we call a unit-test framework (eg. xUnit) and what we call a BDD framework (eg. Cucumber) is much bigger. A BDD framework is much more accurately called an acceptance-test framework, if we're to compare it to a unit-test framework. There are also unit-test frameworks that are designed with BDD in mind -- we call them context/specification test frameworks (as opposed to arrange/act/assert).