Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Test Driven Development with PHPUNIT
Tuan Nguyen, Full Stack Developer
Welcome
Background and Motivation - My Background
Tuan Nguyen
• 7+ years of experience in web development
• Recent position: Softw...
Background and Motivation - Unit Testing
Most of the developers (I know) don’t like Unit Testing. Why?
• “Not having enoug...
Background and Motivation - Unit Testing
Benefits of unit testing
• Unit Tests reduce bugs
• Unit Tests are good documenta...
Background and Motivation - Unit Testing
Unit test is a requirement for Software Engineer
Workflow of TDD and Unit Test
3 Laws of TDD
• Create a test that fails
• Write code to pass the test
• Clean up the code
Demonstration of TDD and Unit Test
The problem to solve
Build a simple calculator with four functions: adding, subtracting...
Demonstration of TDD and Unit Test
The tools we need
• MAMP - Virtual web servers on local machine
• PHP Composer - Manage...
Demonstration of TDD and Unit Test
Create a project
Demonstration of TDD and Unit Test
Install PHPUnit
Demonstration of TDD and Unit Test
Installed PHPUnit in vendor folder
Demonstration of TDD and Unit Test
CalculatorTest.php file with first test case
Demonstration of TDD and Unit Test
First test doesn’t pass
Demonstration of TDD and Unit Test
Create class Calculator.php to pass the test
Demonstration of TDD and Unit Test
setup() and tearDown() function in PHPUnit
Demonstration of TDD and Unit Test
The first real test - testAdd()
Demonstration of TDD and Unit Test
Write code to pass the test - add() function with bug
Demonstration of TDD and Unit Test
Test result of add() function with bug
Demonstration of TDD and Unit Test
Fix the bug to pass the test
Demonstration of TDD and Unit Test
Pass the test for add() function
Demonstration of TDD and Unit Test
Apply these steps for other functions: subtract(), multiply(),
divide()
• Full source o...
References
[1] Top 12 Reasons to Write Unit Tests, http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html
[2] Unit ...
Thank You!
Tuan Nguyen, nganhtuan63@gmail.com
Upcoming SlideShare
Loading in …5
×

Test Driven Development with PHPUnit

884 views

Published on

An example of practicing Test Driven Development with PHPUnit

Published in: Software
  • Be the first to comment

Test Driven Development with PHPUnit

  1. 1. Test Driven Development with PHPUNIT Tuan Nguyen, Full Stack Developer Welcome
  2. 2. Background and Motivation - My Background Tuan Nguyen • 7+ years of experience in web development • Recent position: Software development team lead • Recent project: Enterprise Content Management System
  3. 3. Background and Motivation - Unit Testing Most of the developers (I know) don’t like Unit Testing. Why? • “Not having enough time” • “I understood the requirements and I have implemented them carefully in the code” • “This part is not so important” • “I am lazy”
  4. 4. Background and Motivation - Unit Testing Benefits of unit testing • Unit Tests reduce bugs • Unit Tests are good documentation • Unit Tests improve design • Unit Tests prove your code actually works • Unit Test are vital to regression testing
  5. 5. Background and Motivation - Unit Testing Unit test is a requirement for Software Engineer
  6. 6. Workflow of TDD and Unit Test 3 Laws of TDD • Create a test that fails • Write code to pass the test • Clean up the code
  7. 7. Demonstration of TDD and Unit Test The problem to solve Build a simple calculator with four functions: adding, subtracting, multiplying and dividing. [5]
  8. 8. Demonstration of TDD and Unit Test The tools we need • MAMP - Virtual web servers on local machine • PHP Composer - Manage open source PHP libraries • PHPUnit - PHP test frameworks • Sublime Text - Development-friendly text editor
  9. 9. Demonstration of TDD and Unit Test Create a project
  10. 10. Demonstration of TDD and Unit Test Install PHPUnit
  11. 11. Demonstration of TDD and Unit Test Installed PHPUnit in vendor folder
  12. 12. Demonstration of TDD and Unit Test CalculatorTest.php file with first test case
  13. 13. Demonstration of TDD and Unit Test First test doesn’t pass
  14. 14. Demonstration of TDD and Unit Test Create class Calculator.php to pass the test
  15. 15. Demonstration of TDD and Unit Test setup() and tearDown() function in PHPUnit
  16. 16. Demonstration of TDD and Unit Test The first real test - testAdd()
  17. 17. Demonstration of TDD and Unit Test Write code to pass the test - add() function with bug
  18. 18. Demonstration of TDD and Unit Test Test result of add() function with bug
  19. 19. Demonstration of TDD and Unit Test Fix the bug to pass the test
  20. 20. Demonstration of TDD and Unit Test Pass the test for add() function
  21. 21. Demonstration of TDD and Unit Test Apply these steps for other functions: subtract(), multiply(), divide() • Full source of CalculatorTest.php - http://pastebin.com/pAFAZJDC • Full source of Calculator.php - http://pastebin.com/zXhiVLR8
  22. 22. References [1] Top 12 Reasons to Write Unit Tests, http://www.onjava.com/pub/a/onjava/2003/04/02/javaxpckbk.html [2] Unit testing: Why bother?, http://soundsoftware.ac.uk/unit-testing-why-bother/ [3] The Cycles of TDD, http://blog.cleancoder.com/uncle-bob/2014/12/17/TheCyclesOfTDD.html [4] Test Driven Development (TDD) using PHPUnit, http://diogoosorio.com/blog/test-driven-development-tdd- using-phpunit [5] Test Driven Development With PHP, https://paulund.co.uk/test-driven-development-with-php [6] Let's TDD a Simple App in PHP, http://code.tutsplus.com/tutorials/lets-tdd-a-simple-app-in-php--net-26186
  23. 23. Thank You! Tuan Nguyen, nganhtuan63@gmail.com

×