Agenda
• Something about Test
what? why? why not and how to....
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Test tools introduction, installation and real
coding practice.
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Test tools introduction, installation and real
coding practice.
Unit test, Integration test and Acceptance test.
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Test tools introduction, installation and real
coding practice.
Unit test, Integration test and Acceptance test.
• Test in PHP Frameworks
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Test tools introduction, installation and real
coding practice.
Unit test, Integration test and Acceptance test.
• Test in PHP Frameworks
Zend Framework(still working...)
Agenda
• Something about Test
what? why? why not and how to....
• Test in action!
Test tools introduction, installation and real
coding practice.
Unit test, Integration test and Acceptance test.
• Test in PHP Frameworks
Zend Framework(still working...)
CakePHP(still working...)
What’s Test?
Test != Debug
Why Test?
Every Programmers
Makes Mistakes!!
Why Not Test?
Why not Test?
Why not Test?
• No Time!
Why not Test?
• No Time!
• Boring!
Why not Test?
• No Time!
• Boring!
• Hey! I’m an experienced programmer!
Advantages
Advantages
• The sooner you test for a mistake, the
greater your chance of finding it and the
less it will cost to find and fix.
Advantages
• The sooner you test for a mistake, the
greater your chance of finding it and the
less it will cost to find and fix.
• Good software quality.
Advantages
• The sooner you test for a mistake, the
greater your chance of finding it and the
less it will cost to find and fix.
• Good software quality.
• More confidence in your product(code)!
Then....
What’s the problem?
Unit Test
Unit Test
• Verify the individual units of source code are
working properly.
Unit Test
• Verify the individual units of source code are
working properly.
• A unit is the smallest testable part of an
application.
Unit Test
• Verify the individual units of source code are
working properly.
• A unit is the smallest testable part of an
application.
• May belong to a base/super class, abstract
class or derived/child class.
Integration Test
Integration Test
• Verify high level components of the
application work as expected.
Integration Test
• Verify high level components of the
application work as expected.
• Use mocks, stubs and specialised test classes.
Acceptance Test
Acceptance Test
• Aka “Functional Test”
Acceptance Test
• Aka “Functional Test”
• Ensures that an application behaves as
expected by the client.
Tools
Test Tools
Test Tools
• PHPUnit(Modelled after JUnit)
Test Tools
• PHPUnit(Modelled after JUnit)
• Selenium Remote-Control
PHPUnit
PHPUnit
• Requirements
PHPUnit
• Requirements
• PHP 5.1.4 (or greater) is required, PHP 5.2 is
recommended. PHPUnit 4 will require PHP 5.3.
PHPUnit
• Requirements
• PHP 5.1.4 (or greater) is required, PHP 5.2 is
recommended. PHPUnit 4 will require PHP 5.3.
• more information:
http://www.phpunit.de/wiki/Requirements
How to Test?
How to Test?
• Setup
How to Test?
• Setup
• Execute
How to Test?
• Setup
• Execute
• Verify
How to Test?
• Setup
• Execute
• Verify
• Teardown
PHPUnit Install
PHPUnit Install
• Install from PEAR or manual installation.
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
1. download source
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
1. download source
2. extract and put library in include_path
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
1. download source
2. extract and put library in include_path
3. minor modifications:
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
1. download source
2. extract and put library in include_path
3. minor modifications:
CLI : phpunit
PHPUnit Install
• Install from PEAR or manual installation.
• Manual installation:
1. download source
2. extract and put library in include_path
3. minor modifications:
CLI : phpunit
utils/fileloader.php
Conventions
Conventions
• Function test*() without arguments
ex: testArray(), testmyfunction()
Conventions
• Function test*() without arguments
ex: testArray(), testmyfunction()
• Define in docblock:
ex: @test
Conventions
• Function test*() without arguments
ex: testArray(), testmyfunction()
• Define in docblock:
ex: @test
• Test functions have to be PUBLIC
Remember!!
Remember!!
Comments(docblock) are MEANINGFUL !
Assert Method
Assert Method
They are methods for automatically checking
values and reporting discrepancies.
Test Skeleton
Generator
Test Skeleton
Generator
• Generate a test class file from classes.
Test Skeleton
Generator
• Generate a test class file from classes.
• Usage:
phpunit --skeleton-test testname(.php)
And....vise versa
And....vise versa
• Generate a class skeleton from test case
classes.
And....vise versa
• Generate a class skeleton from test case
classes.
• Usage:
phpunit --skeleton-class classname(.php)
And....vise versa
• Generate a class skeleton from test case
classes.
• Usage:
phpunit --skeleton-class classname(.php)
• Test-Driven Developement.
Test Case Extensions
Test Case Extensions
• Tests for Output
Test Case Extensions
• Tests for Output
• Tests for Performance
Test Case Extensions
• Tests for Output
• Tests for Performance
• Tests for Database
Not finish yet...
Not finish yet...
• Incompleted Tests
Not finish yet...
• Incompleted Tests
• Skip Tests
Unit Test
Unit Test
Unit Test
<?php
require_once 'PHPUnit/Framework.php';
require_once 'testcal.php';
class testcalTest extends PHPUnit_Framework_TestCase
{
protected $object;
protected function setUp()
{
$this->object = new testcal;
}
public function testAdd()
{
$this->assertEquals(2, $this->object->add(1, 1));
}
}
Compose your Tests
• Test Function(phpunit --filter testFunc)
Compose your Tests
• Test Function(phpunit --filter testFunc)
• Test Case
Compose your Tests
• Test Function(phpunit --filter testFunc)
• Test Case
• Test Suite(setUp(), tearDown())
Integration Test
Test Double
Test Double
• When some other components cannot be used in
the test environment...
Test Double
• When some other components cannot be used in
the test environment...
• The Test Double doesn't have to behave exactly
like the real components; it merely has to provide
the same API as the real one so that just let the
test procedure thinks it is the real one!
Acceptance Test
Selenium RC
Selenium RC
• Selenium Remote-Control(RC) is a test
tool that allows you to write automated
user-interface tests for web applications in
any programming language against any
HTTP website using any mainstream
browser.
Selenium RC
• Selenium Remote-Control(RC) is a test
tool that allows you to write automated
user-interface tests for web applications in
any programming language against any
HTTP website using any mainstream
browser.
• More infomation:
http://seleniumhq.org
Test-Driven
Development
• Test before coding.
• Well-designed architecture and APIs.
Test for
PHP Frameworks
Test for
Zend Framework
Test for
Zend Framework
• Application Login TestCase Example.
• Test Case:
The login page should contain one login form
and be displayed to non-authenticated users.
When a user logs in, they should be redirected
to their profile page, and that profile page
should show relevant information.
Test for
CakePHP
Test for
CakePHP
• To be Continued...
References
References
• Manual:
• PHPUnit Manual V3.3
• Articles:
• PHPUnit:
• An Introduction to the Art of Unit Testing in PHP
• Check your PHP code at every level with unit tests
• Acceptance Testing of Web Applications with PHP
• Zend Framework:
• Zend_Test_PHPUnit
0 comments
Post a comment