Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Embracing TDD - A Beginner's approach to get acquainted with TDD workflow

Embracing TDD - A Beginner's approach to get acquainted with TDD workflow

Developers, hate testing with the common reason of boredom, ego or "waste of time". TDD looks overwhelming at first. But, it gives you more than what it takes from you. TDD is considered to be a methodology, but moreover it is an approach, a process that we need to embrace. It’s a rather a shift of paradigm, a BIG change, and from my experience, I can say that no change is easy. I believe, any good change goes through following stages -> Rejection, Ignorance and then, finally ACCEPTANCE. I will share my journey with you on how I embraced TDD; the difficulties I faced and how it made me a better developer. In this talk, I will also give a glimpse of basics of TDD as well as what would be a good place to start implementing it.

Viraj Khatavkar

August 22, 2015
Tweet

More Decks by Viraj Khatavkar

Other Decks in Programming

Transcript

  1. E M B R A C I N G T D D
    - N O V I C E T O I N T E R M E D I AT E

    View Slide

  2. S H O R T S T O RY

    View Slide

  3. I D E A - > I M PA C T

    View Slide

  4. V I R A J K H ATAV K A R
    • Founder, Tantra-Gyan ( T-G )
    • PHP Services
    • Joomla Development
    • WordPress Development
    • UI / UX Design and
    Development
    • Angular

    View Slide

  5. V I R A J K H ATAV K A R
    • Founder, Tantra-Gyan ( T-G )
    • PHP Services
    • Joomla Development
    • WordPress Development
    • UI / UX Design and
    Development
    • Angular
    • @virajkhatavkar
    [email protected]

    View Slide

  6. “As a software developer we should be
    solving the right problems for
    businesses”

    View Slide

  7. C A M B R I D G E U N I V E R S I T Y S T U D Y
    S TAT E S S O F T WA R E B U G S C O S T
    E C O N O M Y $ 3 1 2 B I L L I O N

    View Slide

  8. C H A L L E N G E S
    • Code riddled with defects
    • Nightmare to maintain, slow to develop
    • Failing to meet actual needs

    View Slide

  9. C H A L L E N G E S
    • Code riddled with defects
    • Nightmare to maintain, slow to develop
    • Failing to meet actual needs

    View Slide

  10. C H A L L E N G E S
    • Code riddled with defects
    • Nightmare to maintain, slow to develop
    • Failing to meet actual needs

    View Slide

  11. S O L U T I O N : T E S T I N G

    View Slide

  12. D E S I G N C O D E T E S T

    View Slide

  13. T E S T I N G AT L A S T M AY L E AV E
    H O L E S I N Y O U R T E S T C O V E R A G E

    View Slide

  14. W R I T E A
    FA I L I N G
    T E S T

    View Slide

  15. W R I T E A
    FA I L I N G
    T E S T
    M A K E T H E
    T E S T PA S S

    View Slide

  16. W R I T E A
    FA I L I N G
    T E S T
    M A K E T H E
    T E S T PA S S
    R E FA C T O R

    View Slide

  17. W R I T E A
    FA I L I N G
    T E S T
    M A K E T H E
    T E S T PA S S
    R E FA C T O R
    TDD

    View Slide

  18. O N LY E V E R W R I T E C O D E
    T O F I X A FA I L I N G T E S T

    View Slide

  19. public function test_create_project_class_is_initializable()

    {

    $project = new CreateProject();


    $this->assertInstanceOf(CreateProject::class, $project);

    }
    Create a new class

    View Slide

  20. class CreateProject

    {
    }
    Create a new class

    View Slide

  21. L I V E D E M O

    View Slide

  22. M Y F R U S T R AT I O N W H I L E D O I N G T H I S

    View Slide

  23. T H E S I M P L E I D E A O F W R I T I N G T E S T S
    B E F O R E C O D E D E M O L I S H E D M Y
    C O N C E P T O F P R O G R A M M I N G

    View Slide

  24. “TDD is a mindset, it's the way you
    approach a project not just how you test
    it. It happens to result in a fully
    automated set of unit tests, but these
    unit tests are side effects , not the
    ultimate goal”

    View Slide

  25. PAY M E N T G AT E WAY

    View Slide

  26. public function processPayment(array $paymentDetails)

    {

    $transaction = new \CCAvenue(self::API_ID, self::TRANS_KEY);

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    return $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  27. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()

    {

    $paymentDetails = [

    'amount' => 123.99,

    'card_num' => '4111-1111-1111-1111',

    'exp_date' => '03/2013',

    ];


    $payment = new Payment();

    $result = $payment->processPayment($paymentDetails);


    $this->assertTrue($result);

    }

    View Slide

  28. FAILURES!
    Tests: 1, Assertions: 1, Failures: 1.

    View Slide

  29. public function processPayment(array $paymentDetails)

    {

    $transaction = new \CCAvenue(self::API_ID, self::TRANS_KEY);

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    return $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  30. public function processPayment(array $paymentDetails)

    {

    $transaction = new \CCAvenue(self::API_ID, self::TRANS_KEY);

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  31. T E S T D O U B L E S
    • A test double is an object that can stand n
    for a real object in a test, similar to how a
    stunt double stands in for an actor in a
    movie.

    View Slide

  32. T E S T D O U B L E S
    • Dummy
    • Stub
    • Spy
    • Mock
    • Fake

    View Slide

  33. T E S T D O U B L E S
    • Dummy
    • Stub
    • Spy
    • Mock
    • Fake

    View Slide

  34. M O C K O B J E C T I S A C L O N E O F A N
    O B J E C T W H I C H W E C A N U S E T O
    S I M P L I F Y O U R T E S T S

    View Slide

  35. C L A S S

    View Slide

  36. C L A S S D E P E N D E N C Y

    View Slide

  37. C L A S S
    T E S T
    C L A S S
    D E P E N D E N C Y

    View Slide

  38. C L A S S
    T E S T
    C L A S S
    D E P E N D E N C Y
    M O C K E D
    D E P E N D E N C Y

    View Slide

  39. C L A S S
    T E S T
    C L A S S
    D E P E N D E N C Y
    M O C K E D
    D E P E N D E N C Y

    View Slide

  40. M O C K E RY
    • use Mockery;
    • $mock = Mockery::mock( SomeClass );

    View Slide

  41. public function test_example_of_mocked_class()

    {

    $mock = Mockery::mock( SomeClass::class );


    $mock->shouldReceive('method')->once();

    }

    View Slide

  42. public function processPayment(array $paymentDetails)

    {

    $transaction = new \CCAvenue(self::API_ID, self::TRANS_KEY);

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    return $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  43. $mock = Mockery::mock( CCAvenue::class );

    View Slide

  44. public function processPayment(array $paymentDetails)

    {

    $transaction = new \CCAvenue(self::API_ID, self::TRANS_KEY);

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    return $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  45. D E P E N D E N C Y
    I N J E C T I O N

    View Slide

  46. public function processPayment(\CCAvenue $transaction, array $paymentDetails)

    {

    $transaction->amount = $paymentDetails['amount'];

    $transaction->card_num = $paymentDetails['card_num'];

    $transaction->exp_date = $paymentDetails['exp_date'];


    $response = $transaction->authorizeAndCapture();


    return $response->approved ? true : false;

    }
    #Payment Class

    View Slide

  47. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()

    {

    //Test

    }

    View Slide

  48. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()

    {

    $paymentDetails = [

    'amount' => 123.99,

    'card_num' => '4111-1111-1111-1111',

    'exp_date' => '03/2013',

    ];

    }

    View Slide


  49. $mock = Mockery::mock(\CCAvenue::class);

    View Slide

  50. $mock = Mockery::mock(\CCAvenue::class);


    $mock->shouldReceive('authorizeAndCapture')

    ->once()

    ->andReturn(true);

    View Slide

  51. $mock = Mockery::mock(\CCAvenue::class);


    $mock->shouldReceive('authorizeAndCapture')

    ->once()

    ->andReturn(true);


    $payment = new Payment();

    $result = $payment->processPayment($mock,
    $paymentDetails);

    View Slide

  52. $mock = Mockery::mock(\CCAvenue::class);


    $mock->shouldReceive('authorizeAndCapture')

    ->once()

    ->andReturn(true);


    $payment = new Payment();

    $result = $payment->processPayment($mock,
    $paymentDetails);


    $this->assertTrue($result);

    View Slide

  53. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()

    {

    $paymentDetails = [

    'amount' => 123.99,

    'card_num' => '4111-1111-1111-1111',

    'exp_date' => '03/2013',

    ];


    $mock = Mockery::mock(\CCAvenue::class);


    $mock->shouldReceive('authorizeAndCapture')

    ->once()

    ->andReturn(true);


    $payment = new Payment();

    $result = $payment->processPayment($mock, $paymentDetails);


    $this->assertTrue($result);

    }

    View Slide

  54. “The motivation for mocks is design, not
    testing”

    View Slide

  55. “Mocks should guide you to design how
    your objects talk to each other”

    View Slide

  56. View Slide

  57. - K O N S TA N T I N K U D R A S H O V
    “Example of life is journey, not a
    destination. Life is more about how we
    approach problems and solve them.
    Why don’t we apply the same in
    software design ?”

    View Slide

  58. “A system is not the sum of its parts. It’s
    the product of it’s interactions…”

    View Slide

  59. B E I N G T E S T D R I V E N
    • High quality with TDD
    • Quality comes in many flavours
    • Less time spent fixing defects

    View Slide

  60. W H AT ’ S I N I T F O R M E ?
    • No more long debugging sessions
    • Feeling confident with my work
    • More time for other stuff

    View Slide

  61. T D D - T H E G R A N D I D E A
    • Small incremental steps
    • Evolutionary design
    • Keeping code healthy with refactoring

    View Slide

  62. I M P O R TA N T P O I N T S
    • Your tests should drive your design.
    • TDD exposes your design problems.
    • OOP is about communication between
    objects and TDD helps you in creating
    clear communications.

    View Slide

  63. T E S T D R I V E N D E S I G N

    View Slide

  64. T E S T D R I V E N D E S I G N

    View Slide

  65. Q U E S T I O N S ? ? ?

    View Slide