What is TDD and BDD and why need know about it?

Marcusssbrune
3 min readJan 30, 2023

--

according to wikipedia TDD is:

Test-driven development (TDD) is a software development process relying on software requirements being converted to test cases before software is fully developed, and tracking all software development by repeatedly testing the software against all test cases. This is as opposed to software being developed first and test cases created later.

BDD is defined as:

BDD (behavior-driven development) combines practices from TDD and from ATDD.[30] It includes the practice of writing tests first, but focuses on tests which describe behavior, rather than tests which test a unit of implementation. Tools such as JBehave, Cucumber, Mspec and Specflow provide syntaxes which allow product owners, developers and test engineers to define together the behaviors which can then be translated into automated tests.

After this introduction about TDD and BDD we go talking why it is important that you developer, software engineer… implant this practices in your projects, well we go work with examples practices. I’m use the same code of tutorial “What is better way to deal error the my code?” but now I applicable TDD on the code. This example I have one method that make the sum two numbers, then make the unit test same method it.

Development Environment

  1. Ubuntu OS 22.04
  2. PHP 8.1.14
  3. Composer
  4. Monolog (Library to log manager)
  5. PHPUnit (Library to Unit Tests)

Method that make the sum between two numbers


require './vendor/autoload.php';

use Monolog\Level;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

class Calculator
{
public function sum($a, $b)
{
try {
if(gettype($a) == 'integer' && gettype($b) == 'integer') return $a + $b;

throw new \Exception('error while sum');
} catch(Exception $e) {
$log = new Logger('logger');
$log->pushHandler(new StreamHandler('../log/logger.log', Level::Error));

$log->error($e->getMessage());

// Display error to user
return 'Error while sum';
}
}
}

Method that make unit test the function Sum()


<?php

require './vendor/autoload.php';

require './src/Calculator.php'; // Import file Calculator

use PHPUnit\Framework\TestCase;

class CalculatorTest extends TestCase
{
/**
*
* Test Sum Calculator
*
* @return
*/
public function testSumWithValidIntegers()
{
// Instance Class calculator
$calculator = new Calculator();
// Set function sum for test
$this->assertEquals(5, $calculator->sum(2, 3)); }
}

Method that make unit test

To run your tests is only write on terminal the command:


./vendor/bin/phpunit --verbose tests
Run tests PHPUnit

TDD have one cycle with three steps:

  1. Error: First you write your test for failure
  2. Success: Then you make it success test
  3. Refactor: By end you refactor the code.

This are steps for the you to apply TDD on your code.

What the concept of BDD and why introduce to my team

What is BDD

BDD is one project development model in that you will go describe the functionality comportament, of way that other teams may integrate discussion about improves the project.

By Example describing model functionality with the BDD:

  1. User have ten points
  2. User gain five point then now has fifteen
  3. User then lose two point now it has thirteen points

Some tools that integrate BDD like project development model

  1. SpecFlow
  2. Cucumber
  3. MSpec

These practices help all team the high performance and quality of the end product, keeping a channel the communication that integrate all teams developers, business, testers…

--

--