54
LOGO “ Add your company slogan Keep it Simple Stupid UNIT TESTING Presentation by: Thu.Nguyen

PHPUnit - Unit testing

Embed Size (px)

DESCRIPTION

PHPUnit - Unit testing

Citation preview

Page 1: PHPUnit - Unit testing

LOGO

“ Add your company slogan ”

Keep it Simple Stupid

UNIT TESTING

Presentation by: Thu.Nguyen

Page 2: PHPUnit - Unit testing

KiSS Concept

CONTENT

Q&A

Demo

PHPUnit

Unit Testing

Software Testing

Page 3: PHPUnit - Unit testing

KiSS Concept

SOFTWARE TESTING

Page 4: PHPUnit - Unit testing

What is Software Testing?

Testing is the process of executing a program with intent of finding errors (The art of software testing – Glenford J.Myers)

4

Our program

The output is correct?

I1, I2, I3, …, In, … Expected results

= ?Obtained results

“Inputs”

- No code inspection - No code analysis- No model checking - No bug fixing- No debugging

Page 5: PHPUnit - Unit testing

KiSS Concept

Level of testing

Unit Testing

Integration Testing

Functional Testing

System Testing

Load/Stress Testing

User Accepted Testing

Regression Testing

Programmer

Tester

Page 6: PHPUnit - Unit testing

KiSS Concept

Testing Techniques

Functional Testing(Black box)

Select test cases based on the requirement or design specification

Emphasize on the external behavior of software

Structural Testing(White box)

Select test cases based on the implement of software

Emphasize on the internal structure of software

Page 7: PHPUnit - Unit testing

KiSS Concept

Black box vs White box

Page 8: PHPUnit - Unit testing

KiSS Concept

Process of testing

Test cases

Design test cases

Prepare test data

Run program with test data

Test data

Test results

Test reports

Compare results to test

cases

Page 9: PHPUnit - Unit testing

KiSS Concept

UNIT TESTING

Page 10: PHPUnit - Unit testing

KiSS Concept

What is unit testing?

“In computer programming, unit testing is a method by which individual units of source code are tested to determine if they are fit for use. A unit is the smallest testable part of an application.” (Wikipedia)

A “unit ” is the smallest testable part of an application: a function or class method.

Page 11: PHPUnit - Unit testing

KiSS Concept

Benefit of unit testing

Easy to defect error

Page 12: PHPUnit - Unit testing

KiSS Concept

Benefit of unit testing

Safer refactoring

Page 13: PHPUnit - Unit testing

KiSS Concept

Benefit of unit testing

Automated test

Page 14: PHPUnit - Unit testing

KiSS Concept

Benefit of unit testing

Long term of saving time and money

Page 15: PHPUnit - Unit testing

KiSS Concept

When do you write the test?

Focus on requirements Thinking about how to

code will be consumed Stop coding when meet

requirement Harder initially

Focus on code Thinking about algorithm More refactoring Easier initially

Before coding(TDD) After/During coding

Page 16: PHPUnit - Unit testing

KiSS Concept

How do I test?

Isolate with the code being tested

Short, simple, fast, readable

No conditional logic or loop (if, switch, while)

One test should be one behavior

Page 17: PHPUnit - Unit testing

KiSS Concept

PHPUNIT

Page 18: PHPUnit - Unit testing

KiSS Concept

Introduction

Is a unit testing framework written in PHP, created by Sebastian Bergmann.

Part of the xUnit family of testing frameworks. Integrated/supported

• Zend Studio• Zend Framework• Symfony• Doctrine

Page 19: PHPUnit - Unit testing

KiSS Concept

Installation

Install with PEAR:pear channel-discover pear.phpunit.depear install phpunit/PHPUnit

Update:pear upgrade phpunit/PHPUnit

Xdebug:sudo apt-get install php5-xdebug php5-dev

Set include path in /etc/php5/cli/php.ini include_path = “.:/usr/share/php”

Page 20: PHPUnit - Unit testing

KiSS Concept

Definitions

Test suite

Test suite

Test case

setUp()testMethodtearDown()

/** *@annotation*/testMethod(){assertion}

Report

Test case

setUp()testMethodtearDown()

/** *@annotation*/testMethod(){assertion}

Code coverageLogging

Page 21: PHPUnit - Unit testing

KiSS Concept

Simple test

Page 22: PHPUnit - Unit testing

KiSS Concept

Running test

Go to command line and run:phpunit /path/to/file/test.php or phpunit classname /path/to/file/test.php

Page 23: PHPUnit - Unit testing

KiSS Concept

Running test

Command line

IDE Zend Studio NetBean

Continuous Integration phpUnderControl Cruise Control

Page 24: PHPUnit - Unit testing

KiSS Concept

Result OK: all tests are successful FAILURES: test is fail or error Result of each test:

• . test succeeds• F an assertion fails• E an error• S test has been skipped• I test is marked as being incomplete or not yet

implement

Page 25: PHPUnit - Unit testing

KiSS Concept

Features

@dataProvider

@exception

Fixture: setUp() and tearDown()

Double: stub and mock object

Database

Code coverage

Page 26: PHPUnit - Unit testing

KiSS Concept

Data provider

Is a method that returns an array of values to use in a test.

Makes tests shorter and more concise.

Data provider can use array or file csv.

Page 27: PHPUnit - Unit testing

KiSS Concept

Data provider

Input 1 myfunction() Report 1Result 1 Expected 1

compare

Input 2 myfunction() Report 2Result 2 Expected 2

compare

Input 3 myfunction() Report 3Result 3 Expected 3

compare

Page 28: PHPUnit - Unit testing

KiSS Concept

Data provider

/** @dataProvider*/testMethod($input, $expect){ myfunction()}

Report 1Data Providers

Input 1 expect1

Input 2 expect 2

Input 3 expect 3

Report 2

Report 3

Page 29: PHPUnit - Unit testing

KiSS Concept

Use array

Page 30: PHPUnit - Unit testing

KiSS Concept

Use csv file

Page 31: PHPUnit - Unit testing

KiSS Concept

Exception

Test exception are thrown.

Test expected messages of exception are thrown.

Page 32: PHPUnit - Unit testing

KiSS Concept

Example exception

Page 33: PHPUnit - Unit testing

KiSS Concept

Fixtures

Is constructor method and destructor method in test case

PHPUnit supports 2 methods:• setUp(): the function run when test methods start to

run• tearDown(): the function run after test methods end

Page 34: PHPUnit - Unit testing

KiSS Concept

Fixtures

Start

setUp()

testOne() testTwo() testThree()

tearDown()

End

Page 35: PHPUnit - Unit testing

KiSS Concept

Example fixtures

Page 36: PHPUnit - Unit testing

KiSS Concept

Test double

MyClass

myMethod($object){…$object->otherMethod();…}

Database Connection

File system

Web service

Function depends on other objects, other components

Page 37: PHPUnit - Unit testing

KiSS Concept

MyClass

myMethod(){…other Method…}

Database

File system

Web service

MyClassTest

testMyMethod(){……}

Test double

How to isolate environment ?

Page 38: PHPUnit - Unit testing

KiSS Concept

Test double

MyClass

myMethod(){…other Method…}

Database Mock

File system Mock

Web service Mock

MyClassTest

testMyMethod(){……}

Set mocks

Solution: use copy object Test double

return fixed value

return fixed value

return fixed value

Page 39: PHPUnit - Unit testing

KiSS Concept

Stub and Mock

Set method return values

Test state

Check method calls Check arguments used Test interactions

Stub Mock

Page 40: PHPUnit - Unit testing

KiSS Concept

Example stub object

Page 41: PHPUnit - Unit testing

KiSS Concept

Example mock object

Page 42: PHPUnit - Unit testing

KiSS Concept

Database testing

Set up database connection.

Set up initial dataset.

Provide methods to get dataset from database.

Provide methods to compare two datasets.

Type of dataset Flat XML XML CSV MySQL dump

Page 43: PHPUnit - Unit testing

KiSS Concept

Organizing

Allow to organize tests to test suite to run one time

Use file system: All test files end with ‘Test’. Organize directory structure.

Use configuration file: phpunit.xml

Page 44: PHPUnit - Unit testing

KiSS Concept

Use file system

Page 45: PHPUnit - Unit testing

KiSS Concept

Use phpunit.xml

Page 46: PHPUnit - Unit testing

KiSS Concept

Code Coverage

Help to see what are executed when the tests are run.

Help to find code that is not yet tested.

Help to measure testing completeness.

Page 47: PHPUnit - Unit testing

KiSS Concept

Code Coverage

Page 48: PHPUnit - Unit testing

KiSS Concept

Code Coverage

Red: code is not executedGreen: code is executed

Test cases cover this line code

Grey: dead code

Note: dead code is source code which is executed but whose result is never used.

Page 49: PHPUnit - Unit testing

KiSS Concept

Logging

Test results HTML <log type="testdox-html" target="./log/testdox.html" />

Page 50: PHPUnit - Unit testing

KiSS Concept

Logging

Test result XML <log type="junit" target="./log/logfile.xml" />

Page 51: PHPUnit - Unit testing

KiSS Concept

DEMO

Page 52: PHPUnit - Unit testing

KiSS Concept

References PHPUnit manual

Training course PHPUnit – Nick Belhome,2010

Unit Testing with PHPUnit – Michelangelo van Dam

Ini2it Unit Testing after ZF 1.8 - Michelangelo van Dam

PHPUnit From Zero To Hero – Jeremy Cook

PHPUnit & Continous Intergration – AlexMace

Advanced PHPUnit Testing – Mike Lively

Practical PHP Testing Patterns

http://framework.zend.com/manual/1.12/ru/zend.test.phpunit.db.html

Page 53: PHPUnit - Unit testing

KiSS Concept

QUESTION & ANSWEAR

Q&A

Page 54: PHPUnit - Unit testing

LOGO

“ Add your company slogan ”

Keep it Simple Stupid