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
  2. 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
  3. 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]
  4. 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
  5. 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
  6. 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
  7. 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
  8. S O L U T I O N : T

    E S T I N G
  9. 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
  10. W R I T E A FA I L I

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


    $this->assertInstanceOf(CreateProject::class, $project); 
 } Create a new class
  16. 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
  17. 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
  18. “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”
  19. 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
  20. 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);
 }
  21. 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
  22. 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
  23. 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.
  24. T E S T D O U B L E

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

    S • Dummy • Stub • Spy • Mock • Fake
  26. 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
  27. C L A S S T E S T C

    L A S S D E P E N D E N C Y
  28. 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
  29. 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
  30. M O C K E RY • use Mockery; •

    $mock = Mockery::mock( SomeClass );
  31. 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
  32. 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
  33. D E P E N D E N C Y

    I N J E C T I O N
  34. 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
  35. $mock = Mockery::mock(\CCAvenue::class);
 
 $mock->shouldReceive('authorizeAndCapture')
 ->once()
 ->andReturn(true);
 
 $payment =

    new Payment();
 $result = $payment->processPayment($mock, $paymentDetails);
 
 $this->assertTrue($result);
  36. 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);
 }
  37. - 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 ?”
  38. “A system is not the sum of its parts. It’s

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

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

    D E S I G N