What is BDD and what is its relationship to TDD?

10

In the web , when we search for TDD , sometimes we also come across the abbreviation BDD .

  • What is Behavior Driven Development (BDD) ?
  • What is your relationship with Test Driven Development (TDD) ?
asked by anonymous 15.07.2014 / 13:09

2 answers

16

TDD

TDD focuses on the Unit Test , which means testing every feature within all its possible variations, to ensure that each part of the whole works as expected.

So when creating a class to perform calculations, we will have the following class

public class Calculadora () {
  public float somar (float primeiroNumero, float segundoNumero) { ... }
  public float subtrair (float primeiroNumero, float segundoNumero) { ... }
}

When creating the tests as a developer, I would like to ensure:

- soma de número inteiros
- soma de numeros fracionários   
- soma de números negativos
- soma de primeiro numero positivo e segundo negativo 
- soma de primeiro numero negativo e segundo positivo

and write a test class:

public CalculadoraTests () {
  public void TestarSomaInteiros(){...}
  public void TestarSomaFrancionarios {...}
  ...
}

BDD

BDD is focused on attending features / behaviors , which means defining a scenario, defining the interaction, and the expected result. For example:

Feature: Como alguém que não sabe fazer cálculos, quero que a calculadora me dê o resultados de minhas somas

  Scenario: Somar números inteiros
    Given Eu tenho uma calculadora com visor vazio
    When Eu solicitar a soma de 2 e 2
    Then Ela deve me retornar o valor 4

This pattern is called Gherkin and so you define the expected Behavior

Then just code: ( example in javascript pq is what I opened here )

var calc = new Calculadora();

this.Given(/^Eu tenho uma calculadora com visor vazio$/, function (callback) {

    calc.limpar;
    callback();
});

this.When(/^Eu solicitar a soma de (\d+) e (\d+)$/, function (num1, num2, callback) {

    calc.soma(num1, num2);
    callback();
});

this.Then(/^Ela deve me retornar o valor (\d+)$/, function (resultadoEsperado, callback) {

    assert.equal(calc.resultado(), resultadoEsperado, 'O resultado deveria ser ' + resultadoEsperado + ' mas foi ' + calc.resultado());
    callback();
});

Then with these same defined steps I can execute several scenarios

  Scenario: Somar números inteiros
    Given Eu tenho uma calculadora com visor vazio
    When Eu solicitar a soma de 2 e 2
    Then Ela deve me retornar o valor 4

  Scenario: Somar números negativos
    Given Eu tenho uma calculadora com visor vazio
    When Eu solicitar a soma de -2 e -2
    Then Ela deve me retornar o valor -4

  Scenario: Somar primeiro numero negativo e segundo positivo
    Given Eu tenho uma calculadora com visor vazio
    When Eu solicitar a soma de -2 e 2
    Then Ela deve me retornar o valor 0

Conclusion

I believe that TDD and BDD are not necessarily mutually exclusive, some complex rules can be tested on a unitary basis while behaviors should be tested as such.

    
15.07.2014 / 14:13
8
The TDD "play" is that there are a lot of developers focused on "how" when writing their unit tests, so they ended up with a lot of fragile tests that did nothing more than confirm that the system does the that makes.

BDD will provide a new vocabulary and thus focus to write a unit test. Basically, it's a characteristic approach driven to TDD . So basically BDD "literally" is just TDD with all terminology replaced with terminology terminology examples of behavior

In this LINK has a similar question and the answer credits.

HERE has another explanatory text about.

    
15.07.2014 / 13:20