Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ATDD versus BDD and the proper use of a framework

Tags:

I am just getting into the concept of BDD and have listened to Scott Bellware's talk with the Herding Code guys. I have been playing around with SpecFlow some and like it pretty well.

I understand the distinction between ATDD and TDD as described in the blog post Classifying BDD Tools (Unit-Test-Driven vs. Acceptance Test Driven) and a bit of BDD history, but that leads me to a question.

As described, isn't using a BDD tool (such as MSpec) just another unit testing framework? It seems to me that it is.

Furthermore, this seems to suggest that using SpecFlow to spec out the lower level components (such as your repositories and services) would be wrong. If I can use the same tool for both ATDD and TDD of lower level components, why shouldn't I? There seems to still be some blurry lines here that I feel like I'm not quite understanding.

like image 821
Brian McCord Avatar asked Jul 29 '10 03:07

Brian McCord


People also ask

What is the difference between BDD and TDD and ATDD?

The TDD approach focuses on the implementation of a feature. Whereas BDD focuses on the behavior of the feature, and ATDD focuses on capturing the requirements. To implement TDD we need to have technical knowledge.

Is BDD a framework or methodology?

Behavior Driven Development (BDD) Framework enables software testers to complete test scripting in plain English. BDD mainly focuses on the behavior of the product and user acceptance criteria.

What is the difference between TDD and BDD framework?

TDD is a development practice while BDD is a team methodology. In TDD, the developers write the tests while in BDD the automated specifications are created by users or testers (with developers wiring them to the code under test.) For small, co-located, developer-centric teams, TDD and BDD are effectively the same.

Which one is best BDD or TDD?

BDD is in a more readable format by every stakeholder since it is in English. Unlike TDD, test cases are written in programming languages such as Ruby and Java. BDD explains the behavior of an application for the end-user while TDD focuses on how functionality is implemented.


1 Answers

The Quick Answer

One very important point to bring up is that there are two flavors of Behavior Driven Development. The two flavors are xBehave and xSpec.

xBehave BDD: SpecFlow

SpecFlow (very similar to cucumber from the Ruby stack) is excellent in facilitating xBehave BDD tests as Acceptance Criteria. It does not however provide a good way to write behavioral tests on a unit level. There are a few other xBehave testing frameworks, but SpecFlow has gotten a lot of traction.

xSpec BDD: NSpec

For behavior driven development on a unit level, I would recommend NSpec (inspired directly by RSpec for Ruby). You can accomplish BDD on a unit level by simply using NUnit or MSTest...but they kinda fall short (it's really hard to build up contexts incrementally). MSpec is also an option, there has been a lot of work put into it, but there are just somethings that are just simpilier in NSpec (you can build up context incrementally in MSpec, but it requires inheritance which can become complex).

The Long Answer

The two flavors of BDD primarily exist because of the orthogonal benefits they provide.

Pros and Cons of xBehave (GWT Syntax)

Pros

  • helps facilitate a conversations with the business through a common dialect called (eg. Given ...., And Given ...., When ......, And When ..... , Then ...., And Then)
  • the common dialect can then be mapped to executable code which proves to the business that you actually finished what you said you'd finish
  • the dialect is constricting, so the business has to disambiguate requirements and make it fit into the sentences.

Cons

  • While the xBehave approach is good for driving high level Acceptance Criteria, the cycles needed to map English to executable code via attributes makes it infeasible for driving out a domain at the unit level.
  • Mapping the common dialect to tests is PAINFUL (ramp up on your regex). Each sentence the business creates must be mapped to an executable method via attributes.
  • The common dialect must be tightly controlled so that managing the mapping doesn't get out of hand. Any time you change a sentence, you have to find method that directly relates to that sentence and fix the regex matching.

Pros and Cons of xSpec (Context/Specification)

Pros

  • Allows the developer to build up context incrementally. A context can be set up for a test and some assertions can be performed against that context. You can then specify more context (building upon the context that already exists) and then specify more tests.
  • No constricting language. Developers can be more expressive about how a certain part of a system behaves.
  • No mapping needed between English and a common dialect (because there isn't one).

Cons

  • Not as approachable by the business. Let's face it, the business don't like to disambiguate what they want. If we gave them a context based approach to BDD then the sentence would just read "Just make it work".
  • Everything is in the code. The context documentation is intertwined within the code (that's why we don't have to worry about mapping english to code)
  • Not as readable given a less restrictive verbiage.

Samples

The Bowling Kata is a pretty good example.

SpecFlow Sample

Here is what the specification would look like in SpecFlow (again, this is great as an acceptance test, because it communicates directly with the business):

Feature File

The feature file is the common dialect for the test.

 Feature: Score Calculation    In order to know my performance   As a player   I want the system to calculate my total score  Scenario: Gutter game   Given a new bowling game   When all of my balls are landing in the gutter   Then my total score should be 0 
Step Definition File

The step definition file is the actual execution of the test, this file includes the mappings for SpecFlow

  [Binding] public class BowlingSteps {     private Game _game;      [Given(@"a new bowling game")]     public void GivenANewBowlingGame()     {         _game = new Game();     }      [When(@"all of my balls are landing in the gutter")]     public void WhenAllOfMyBallsAreLandingInTheGutter()     {         _game.Frames = "00000000000000000000";     }      [Then(@"my total score should be (\d+)")]     public void ThenMyTotalScoreShouldBe(int score)     {         Assert.AreEqual(0, _game.Score);     } }  

NSpec Sample, xSpec, Context/Specification

Here is a NSpec example of the same bowling kata:

  class describe_BowlingGame : nspec {     Game game;      void before_each()     {         game = new Game();     }      void when_all_my_balls_land_in_the_gutter()     {         before = () =>         {             game.Frames = "00000000000000000000";         };          it["should have a score of 0"] = () => game.Score.should_be(0);     } }  

So Yea...SpecFlow is cool, NSpec is cool

As you do more and more BDD, you'll find that both the xBehave and xSpec flavors of BDD are needed. xBehave is more suited for Acceptance Tests, xSpec is more suited for unit tests and domain driven design.

Relevant Links

  • RSpec vs Cucumber (RSpec stories)
  • BDD with Cucumber and rspec - when is this redundant?
  • NSpec Project Site
  • Continuous Testing
  • Introduction to BDD and Mocking
  • BDD using NUnit and Moq
like image 149
Amir Avatar answered Oct 19 '22 08:10

Amir