What is BDD and its relation to TDD?

Asked

Viewed 746 times

10

In the web, when we searched for TDD, sometimes we also come across the acronym BDD.

  • What is Behavior Driven Development (BDD)?
  • What is your relationship with Test Driven Development (TDD)?

2 answers

15


TDD

TDD is focused on Unitary Test, which means testing each functionality 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 class of tests:

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

BDD

The BDD is focused on meet 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 (Behavior)

then just code: (example in javascript pq is what had 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();
});

Hence, with these same steps defined I can perform 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

Completion

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

8

The "move" of TDD is that there are many developers focused on "how" when writing their unit tests, so that they ended up with too many fragile tests that did nothing more than confirm that the system does what it does.

BDD will provide a new vocabulary and thus concentrate to write a unit test. Basically, it is a feature approach conducted to TDD. So basically BDD "literally" is just TDD with all the terminology of tests replaced by terminology examples of behavior

In that LINK has a similar question and the credits of the answer.

HERE has another explanatory text on.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.