test driven development with phpunit
TRANSCRIPT
Test Driven Development with PHPUNITTuan Nguyen, Full Stack Developer
Welcome
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
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”
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
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, multiplying and dividing. [5]
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
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 TestFirst test doesn’t pass
Demonstration of TDD and Unit TestCreate class Calculator.php to pass the test
Demonstration of TDD and Unit Testsetup() and tearDown() function in PHPUnit
Demonstration of TDD and Unit TestThe first real test - testAdd()
Demonstration of TDD and Unit TestWrite code to pass the test - add() function with bug
Demonstration of TDD and Unit TestTest result of add() function with bug
Demonstration of TDD and Unit TestFix the bug to pass the test
Demonstration of TDD and Unit TestPass the test for add() function
Demonstration of TDD and Unit TestApply 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
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
Thank You!Tuan Nguyen, [email protected]