test driven development with phpunit

23
Test Driven Development with PHPUNIT Tuan Nguyen, Full Stack Developer Welcome

Upload: tuan-nguyen

Post on 14-Apr-2017

310 views

Category:

Software


4 download

TRANSCRIPT

Page 1: Test Driven Development with PHPUnit

Test Driven Development with PHPUNITTuan Nguyen, Full Stack Developer

Welcome

Page 2: Test Driven Development with PHPUnit

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

Page 3: Test Driven Development with PHPUnit

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”

Page 4: Test Driven Development with PHPUnit

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

Page 5: Test Driven Development with PHPUnit

Background and Motivation - Unit Testing

Unit test is a requirement for Software Engineer

Page 6: Test Driven Development with PHPUnit

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

Page 7: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Test

The problem to solve

Build a simple calculator with four functions: adding, subtracting, multiplying and dividing. [5]

Page 8: Test Driven Development with PHPUnit

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

Page 9: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Test

Create a project

Page 10: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Test

Install PHPUnit

Page 11: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Test

Installed PHPUnit in vendor folder

Page 12: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Test

CalculatorTest.php file with first test case

Page 13: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestFirst test doesn’t pass

Page 14: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestCreate class Calculator.php to pass the test

Page 15: Test Driven Development with PHPUnit

Demonstration of TDD and Unit Testsetup() and tearDown() function in PHPUnit

Page 16: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestThe first real test - testAdd()

Page 17: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestWrite code to pass the test - add() function with bug

Page 18: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestTest result of add() function with bug

Page 19: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestFix the bug to pass the test

Page 20: Test Driven Development with PHPUnit

Demonstration of TDD and Unit TestPass the test for add() function

Page 21: Test Driven Development with PHPUnit

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

Page 22: Test Driven Development with PHPUnit

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

Page 23: Test Driven Development with PHPUnit

Thank You!Tuan Nguyen, [email protected]