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.
LOGO
“ Add your company slogan ”
Keep it Simple Stupid
Presentation by: Thu.Nguyen
CONTENT
KiSS Concept
Q&A
Demo
PHPUnit
Unit Testing
Software Testing
KiSS Concept
What is Software Testing?
 Testing is the process of executing a program with intent
of finding errors (The art of softwa...
Level of testing
KiSS Concept
Unit Testing
Integration Testing
Functional Testing
System Testing
Load/Stress Testing
User ...
Testing Techniques
Functional Testing
(Black box)
 Select test cases based
on the requirement or
design specification
 E...
Black box vs White box
KiSS Concept
Process of testing
KiSS Concept
Test
cases
Design test
cases
Prepare test
data
Run program
with test data
Test
data
Test
r...
KiSS Concept
What is unit testing?
KiSS Concept
 “In computer programming, unit testing is a method by which
individual units of sourc...
Benefit of unit testing
 Easy to defect error
KiSS Concept
Benefit of unit testing
 Safer refactoring
KiSS Concept
Benefit of unit testing
 Automated test
KiSS Concept
Benefit of unit testing
 Long term of saving time and money
KiSS Concept
When do you write the test?
 Focus on requirements
 Thinking about how to
code will be consumed
 Stop coding when meet
...
How do I test?
 Isolate with the code being tested
 Short, simple, fast, readable
 No conditional logic or loop (if, sw...
KiSS Concept
Introduction
KiSS Concept
 Is a unit testing framework written in PHP, created by
Sebastian Bergmann.
 Part of the xUnit...
Installation
KiSS Concept
Install with PEAR:
pear channel-discover pear.phpunit.de
pear install phpunit/PHPUnit
Update:
pe...
Definitions
KiSS Concept
Test suite
Test suite
Test case
setUp()
testMethod
tearDown()
/*
*
*@annotation
*/
testMethod(){
...
Simple test
KiSS Concept
Running test
KiSS Concept
Go to command line and run:
phpunit /path/to/file/test.php or phpunit classname
/path/to/file/te...
Running test
 Command line
 IDE
 Zend Studio
 NetBean
 Continuous Integration
 phpUnderControl
 Cruise Control
KiSS...
Result
 OK: all tests are successful
 FAILURES: test is fail or error
 Result of each test:
• . test succeeds
• F an as...
Features
 @dataProvider
 @exception
 Fixture: setUp() and tearDown()
 Double: stub and mock object
 Database
 Code c...
Data provider
 Is a method that returns an array of values to use in a test.
 Makes tests shorter and more concise.
 Da...
Data provider
KiSS Concept
Input 1 myfunction() Report 1Result 1 Expected 1
compare
Input 2 myfunction() Report 2Result 2 ...
Data provider
KiSS Concept
/*
* @dataProvider
*/
testMethod($input, $expect)
{
myfunction()
}
Report 1
Data Providers
Inpu...
Use array
KiSS Concept
Use csv file
KiSS Concept
Exception
 Test exception are thrown.
 Test expected messages of exception are thrown.
KiSS Concept
Example exception
KiSS Concept
Fixtures
 Is constructor method and destructor method in test case
 PHPUnit supports 2 methods:
• setUp(): the function ...
Fixtures
KiSS Concept
Start
setUp()
testOne() testTwo() testThree()
tearDown()
End
KiSS Concept
Example fixtures
Test double
KiSS Concept
MyClass
myMethod($object)
{
…$object->otherMethod();
…
}
Database
Connection
File system
Web serv...
KiSS Concept
MyClass
myMethod()
{
…other Method
…
}
Database
File system
Web service
MyClassTest
testMyMethod()
{
…
…
}
Te...
Test double
KiSS Concept
MyClass
myMethod()
{
…other Method
…
}
Database Mock
File system Mock
Web service Mock
MyClassTes...
Stub and Mock
 Set method return
values
 Test state
 Check method calls
 Check arguments used
 Test interactions
KiSS...
Example stub object
KiSS Concept
Example mock object
KiSS Concept
Database testing
 Set up database connection.
 Set up initial dataset.
 Provide methods to get dataset from database.
...
Organizing
 Allow to organize tests to test suite to run one time
 Use file system:
 All test files end with „Test‟.
 ...
Use file system
KiSS Concept
Use phpunit.xml
KiSS Concept
Code Coverage
KiSS Concept
 Help to see what are executed when the tests are run.
 Help to find code that is not yet tes...
Code Coverage
KiSS Concept
Code Coverage
KiSS Concept
Red: code is not executedGreen: code is executed
Test cases cover this line code
Grey: dead cod...
Logging
Test results HTML
 <log type="testdox-html" target="./log/testdox.html" />
KiSS Concept
Logging
Test result XML
 <log type="junit" target="./log/logfile.xml" />
KiSS Concept
KiSS Concept
References
 PHPUnit manual
 Training course PHPUnit – Nick Belhome,2010
 Unit Testing with PHPUnit – Michelangelo van D...
QUESTION & ANSWEAR
KiSS Concept
LOGO
“ Add your company slogan ”
Keep it Simple Stupid
Upcoming SlideShare
Loading in …5
×

PHPUnit - Unit testing

3,613 views

Published on

PHPUnit - Unit testing

Published in: Technology

PHPUnit - Unit testing

  1. 1. LOGO “ Add your company slogan ” Keep it Simple Stupid Presentation by: Thu.Nguyen
  2. 2. CONTENT KiSS Concept Q&A Demo PHPUnit Unit Testing Software Testing
  3. 3. KiSS Concept
  4. 4. 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
  5. 5. Level of testing KiSS Concept Unit Testing Integration Testing Functional Testing System Testing Load/Stress Testing User Accepted Testing Regression Testing Programmer Tester
  6. 6. 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 KiSS Concept
  7. 7. Black box vs White box KiSS Concept
  8. 8. Process of testing KiSS Concept Test cases Design test cases Prepare test data Run program with test data Test data Test results Test reports Compare results to test cases
  9. 9. KiSS Concept
  10. 10. What is unit testing? KiSS Concept  “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.
  11. 11. Benefit of unit testing  Easy to defect error KiSS Concept
  12. 12. Benefit of unit testing  Safer refactoring KiSS Concept
  13. 13. Benefit of unit testing  Automated test KiSS Concept
  14. 14. Benefit of unit testing  Long term of saving time and money KiSS Concept
  15. 15. 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 KiSS Concept Before coding(TDD) After/During coding
  16. 16. 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 KiSS Concept
  17. 17. KiSS Concept
  18. 18. Introduction KiSS Concept  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
  19. 19. Installation KiSS Concept Install with PEAR: pear channel-discover pear.phpunit.de pear 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”
  20. 20. Definitions KiSS Concept Test suite Test suite Test case setUp() testMethod tearDown() /* * *@annotation */ testMethod(){ assertion } Report Test case setUp() testMethod tearDown() /* * *@annotation */ testMethod(){ assertion } Code coverage Logging
  21. 21. Simple test KiSS Concept
  22. 22. Running test KiSS Concept Go to command line and run: phpunit /path/to/file/test.php or phpunit classname /path/to/file/test.php
  23. 23. Running test  Command line  IDE  Zend Studio  NetBean  Continuous Integration  phpUnderControl  Cruise Control KiSS Concept
  24. 24. 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 KiSS Concept
  25. 25. Features  @dataProvider  @exception  Fixture: setUp() and tearDown()  Double: stub and mock object  Database  Code coverage KiSS Concept
  26. 26. 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. KiSS Concept
  27. 27. Data provider KiSS Concept 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
  28. 28. Data provider KiSS Concept /* * @dataProvider */ testMethod($input, $expect) { myfunction() } Report 1 Data Providers Input 1 expect1 Input 2 expect 2 Input 3 expect 3 Report 2 Report 3
  29. 29. Use array KiSS Concept
  30. 30. Use csv file KiSS Concept
  31. 31. Exception  Test exception are thrown.  Test expected messages of exception are thrown. KiSS Concept
  32. 32. Example exception KiSS Concept
  33. 33. 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 KiSS Concept
  34. 34. Fixtures KiSS Concept Start setUp() testOne() testTwo() testThree() tearDown() End
  35. 35. KiSS Concept Example fixtures
  36. 36. Test double KiSS Concept MyClass myMethod($object) { …$object->otherMethod(); … } Database Connection File system Web service Function depends on other objects, other components
  37. 37. KiSS Concept MyClass myMethod() { …other Method … } Database File system Web service MyClassTest testMyMethod() { … … } Test double How to isolate environment ?
  38. 38. Test double KiSS Concept 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
  39. 39. Stub and Mock  Set method return values  Test state  Check method calls  Check arguments used  Test interactions KiSS Concept Stub Mock
  40. 40. Example stub object KiSS Concept
  41. 41. Example mock object KiSS Concept
  42. 42. 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 KiSS Concept
  43. 43. 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 KiSS Concept
  44. 44. Use file system KiSS Concept
  45. 45. Use phpunit.xml KiSS Concept
  46. 46. Code Coverage KiSS Concept  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.
  47. 47. Code Coverage KiSS Concept
  48. 48. Code Coverage KiSS Concept 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.
  49. 49. Logging Test results HTML  <log type="testdox-html" target="./log/testdox.html" /> KiSS Concept
  50. 50. Logging Test result XML  <log type="junit" target="./log/logfile.xml" /> KiSS Concept
  51. 51. KiSS Concept
  52. 52. 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 KiSS Concept
  53. 53. QUESTION & ANSWEAR KiSS Concept
  54. 54. LOGO “ Add your company slogan ” Keep it Simple Stupid

×