Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

up vote 1 down vote accepted

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).

share|improve this answer

In software development you usually start with a specification which, ideally, comes from the customer. In that specification, among other things, you will find user stories that define the behavior of the application. The example that you gave is an example of such user story. This is where BDD kicks in. It gives you the language (Gherkin) and tools (Cucumber, SpecFlow, JBehave) that enable you, the developer, to translate these user stories into features and scenarios that are easy to understand by a non-technical person (eg. customers, stakeholders etc.) This gives you the option of training a person to write these specifications, leaving you (or a tester) the job of implementing the tests.

To continue on your example, you start by defining a feature and a scenario (or more), using what is called the GWT (Given-When-Then) syntax:

Feature: Read blog
    In order to read the blog
    As a registered user
    I want to see a list of blog entries

Scenario: Access the blog
    Given: I am logged in (I am logged in as, if you plan on testing specific users)
    When: I navigate to the blog
    Then: I see a list of blog entries

In this case, Cucumber will generate tests for each step in your scenario. In the tests you will write automation code (Selenium) that will make your browser act on the behavior specified in the scenario. This is extremely useful when you want to do acceptance testing or regression testing.

share|improve this answer
    
In the unit tests you will write automation code (Selenium) that will make your browser act on the behavior specified in the scenario. I come from the python world, and AFAIK writing selenium test means integration testing, not unit testing. –  whatf Sep 21 '12 at 8:39
    
@whatf - updated the answer, thanks for the heads up. –  devnull Sep 21 '12 at 8:51
    
so then where will you write, unit tests? –  whatf Sep 21 '12 at 10:20
    
Well, you see the interface as an integration of components, therefore the automation code that you write in the test methods generated by Cucumber will constitute integration tests. The unit tests will be done for the components that make the rendering of the interface possible. On the other hand, I see the interface as a collection of behaviors (units), therefore I see the automation code that I write as an unit test. –  devnull Sep 21 '12 at 10:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.