Save 37% off PRO during our Black Friday Sale! »

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.

9c3934401516e367891d48ccfefbb4f0?s=128

Viraj Khatavkar

August 22, 2015
Tweet

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
  2. S H O R T S T O RY

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

    T
  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
  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 • viraj@tantra-gyan.com
  6. “As a software developer we should be solving the right

    problems for businesses”
  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
  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
  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
  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
  11. S O L U T I O N : T

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

    T E S T
  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
  14. W R I T E A FA I L I

    N G T E S T
  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
  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
  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
  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
  19. public function test_create_project_class_is_initializable()
 { 
 $project = new CreateProject();
 


    $this->assertInstanceOf(CreateProject::class, $project); 
 } Create a new class
  20. class CreateProject
 { } Create a new class

  21. L I V E D E M O

  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
  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
  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”
  25. PAY M E N T G AT E WAY

  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
  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);
 }
  28. FAILURES! Tests: 1, Assertions: 1, Failures: 1.

  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
  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
  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.
  32. T E S T D O U B L E

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

    S • Dummy • Stub • Spy • Mock • Fake
  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
  35. C L A S S

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

    D E N C Y
  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
  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
  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
  40. M O C K E RY • use Mockery; •

    $mock = Mockery::mock( SomeClass );
  41. public function test_example_of_mocked_class()
 {
 $mock = Mockery::mock( SomeClass::class );
 


    $mock->shouldReceive('method')->once();
 }
  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
  43. $mock = Mockery::mock( CCAvenue::class );

  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
  45. D E P E N D E N C Y

    I N J E C T I O N
  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
  47. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()
 {
 //Test
 }

  48. public function testProcessPaymentReturnsTrueOnSuccessfulPayment()
 {
 $paymentDetails = [
 'amount' => 123.99,


    'card_num' => '4111-1111-1111-1111',
 'exp_date' => '03/2013',
 ];
 }
  49. 
 $mock = Mockery::mock(\CCAvenue::class);

  50. $mock = Mockery::mock(\CCAvenue::class);
 
 $mock->shouldReceive('authorizeAndCapture')
 ->once()
 ->andReturn(true);

  51. $mock = Mockery::mock(\CCAvenue::class);
 
 $mock->shouldReceive('authorizeAndCapture')
 ->once()
 ->andReturn(true);
 
 $payment =

    new Payment();
 $result = $payment->processPayment($mock, $paymentDetails);
  52. $mock = Mockery::mock(\CCAvenue::class);
 
 $mock->shouldReceive('authorizeAndCapture')
 ->once()
 ->andReturn(true);
 
 $payment =

    new Payment();
 $result = $payment->processPayment($mock, $paymentDetails);
 
 $this->assertTrue($result);
  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);
 }
  54. “The motivation for mocks is design, not testing”

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

    to each other”
  56. None
  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 ?”
  58. “A system is not the sum of its parts. It’s

    the product of it’s interactions…”
  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
  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
  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
  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.
  63. T E S T D R I V E N

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

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

    ? ?