phpunit from a developer's perspective

Download PHPUnit from a developer's perspective

If you can't read please download the document

Upload: tushar-joshi

Post on 17-May-2015

2.621 views

Category:

Education


7 download

DESCRIPTION

Introduction to PHPUnit unit testing tool for PHP presented by Tushar Joshi, Nagpur @ PHPMeet help in Nagpur on 19th June 2010

TRANSCRIPT

  • 1. PHPUnit A developer's perspective By Tushar Joshi, Nagpur Presented at: 19 June 2010, 4pm, PHPMeet Nagpur #ngpphpmeet

2. Assumptions for this presentation

  • No earlier knowledge of PHPUnit

3. PHP Developers 4. Knowledge of Object Oriented Programming 5. Things covered

  • Conceptual introduction to PHPUnit

6. Discussion about need of unit testing 7. Case for PHPUnit 8. Demonstration of working of PHPUnit (not part of slides but done side by side while presenting) 9. Things not covered

  • History of PHPUnit, history of unit testing frameworks

10. Installation of PHP, PHPUnit, PEAR, Xdebug, NetBeans IDE 11. Configuration of the development environment 12. Detailed usage of PHPUnit, all cases which can be tested 13. Are there any bugs?

  • Developer says:
  • I have checked the code many times

14. There cannot be any bugs now 15. Manager believes the first time Courtesy:Sam Fraser - Smith 16. Client sends issues

  • Developer says:
  • Oops, I don't know how I slipped that one

17. Never thought that value will be passed to this function 18. Next time manager will be skeptical to believe easily Courtesy:cheerfulmonk 19. Introducing PHPUnit

  • Tool to unit test your PHP code

20. To re-test the code after any modifications 21. Tool to ensure your code always work for given inputs

  • Can be used for Object Oriented PHP code

22. Is PHPUnit Easy?

  • You cannot use the tool unless you know it

23. Learning a tool is a skill building activity 24. Assuming you know the tool, it will save you always

  • PHPUnit is written in PHP

25. You know PHP 26. PHPUnit Overhead?

  • Developers tool to verify his code

27. No need to show test classes if not needed 28. If tool is readily available, it is a matter of habit

  • Peace of mind

29. Way to prove your code works in all situations 30. Opportunity to trap hidden bugs before client finds them 31. Life Saver 32. Case for PHPUnit

  • What will you choose between
  • Text Editor

33. Syntax highlighting editor with context help and intelli-sense (CTRL+SPACE) What will you choose between

  • Testing methods by eyes

34. Testing by a tool, with advanced facilities 35. Sample PHP class

  • Let us test this class

36. Starting with getBalance method 37. Opportunity to think behavior of getBalance 38.

  • Test Class

39. Result Red or Green Sample Test 40. Available Assertions 41. Using Constraints

  • $this->lessThan(3000)

42. This creates a constraint expectation 43. $this->assertThat checks whether the value is according to the expectation 44. Available Constraints 45. Testing Exceptions

  • @expectedException

46. To check whether exception was thrown 47. Data Providers

  • Way to create multiple data sets

48. Sets of parameters for which the function must run correctly 49. Reduces effort for writing multiple test methods 50. Mock Objects and Expectations

  • Quickest way to test dependencies

51. $object = $this->getMock('NameOfClass'); 52. $object->expects( invocationExpectation ) ->method( methodExpectation )[->with( parameterExpectation )][->will( returnValue Expectation)] 53. Available Invocation Expectations

  • $this->any() - example

54. Parameters and Return Values

  • Used with the with and will methods of mock object

55. $this->equals(val) type of constraints 56. $this->onConsecutiveCalls(val, val2, val3,..)

  • List of values which will be used for each consecutive next call

57. Advanced Features

  • Virtual File System

58. DB testing with DBUnit 59. Mocking Web services 60. Testing for console output 61. References

  • PHPUnit site
  • http://www.phpunit.de

PHPUnit Manual

  • http://www.phpunit.de/manual/3.5/en/index.html

NetBeans IDE Site

  • http://www.netbeans.org