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.

Unit testing in PHP

Unit testing in PHP

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all
  • Be the first to comment

  • Be the first to like this

Unit testing in PHP

  1. 1. UNIT TESTING IN PHP @chonla October 3, 2014
  2. 2. ARE YOU HURT? When you add a new feature or just change a single character and then … ! ◦ bugs appear ◦ your friend’s code is not working anymore ◦ it conflicts with other features
  3. 3. ARE YOU TIRED? When you add a new feature or just change a single character and then … ! • You have to manually retest everything from start to ensure it is still working • You think it is working as you THINK but it is NOT
  4. 4. You do not deserve that! Unit testing can help you.
  5. 5. WHY? ◦ If you test every single piece of code well ▪ When you add a new code, the existing test cases will tell you early if there is a conflict. ◦ If you test all possible test cases ▪ When you add a new code, the existing test cases will tell you early if there is a conflict. ◦ If you create unit test cases ▪ It can be automatic rerun as much as you want ◦ If you write a good unit test ▪ It should be simple and easy to test
  6. 6. What help us to create quality unit test? • PHP • PHPUnit • Dependency Injection • Code coverage • Belief • and Conscious
  7. 7. With PHPUnit, you can ! • Test your methods/functions • Mock your classes • Stub your classes • Stub your functions
  8. 8. OUTLINE • What is unit testing? • What is to be tested? • What does not need to be tested? • Benefits of unit testing • Unit testing FIRST principles • Unit testing measurement • Writing unit testing before/after? • Test structure • Testing tools • Possible questions • References
  9. 9. WHAT IS UNIT TESTING? A software testing method to test individual unit of source code — a method in class or a piece of code
  10. 10. WHAT IS TO BE TESTED? Everything
  11. 11. WHAT DOES NOT NEED TO BE TESTED? •Getter/Setter •They are so trivial. •Third party packages •Don’t repeat unnecessary testing. They have been already tested by their developers.
  12. 12. BENEFITS OF UNIT TESTING •Early problem discovery •It is better if we can find the error/problem before staging or production stage. •Facilitates change •Unit testing allows developers to refactor code anytime. It ensures the code still works correctly. •Simplifies integration •Unit testing increases confidence in integration. If everything is tested before integration, integration should be easier. •Self documentation •Let the test tell the story. •It visualises the design •By doing TDD, unit testing helps developers to design classes, to smell code mess and to refactor the code
  13. 13. UNIT TESTING FIRST PRINCIPLES •Fast — Each test should run fast, really fast. •Isolated — It should have no dependency involved. •Repeatable — It should be idempotent. •Self-verifying — Result should be just pass/fail, no extra investigation. •Timely — Every code change should result a new test.
  14. 14. UNIT TESTING MEASUREMENT •Line of code •Branch Code coverage DOES NOT guarantee that your application does not contain bugs or has good quality.
  15. 15. WRITE UNIT TESTING BEFORE/AFTER? •After •You test what you code — How can you validate & verify what you have done? •Before •You code what you test — TDD.
  16. 16. TEST STRUCTURE •Arrange — Given •Act — When •Assert — Then
  17. 17. TESTING TOOLS •PHPUnit •Mockery •Codeception •SimpleTest •PHPSpec
  18. 18. POSSIBLE QUESTIONS •What does unit test coverage tell you? •It tells you how many piece of code was tested. •Is high coverage good? •No, if it is not a good quality testing. •Should code coverage be in KPI? •No, doing 100% code coverage is not hard. Assert free testing can achieve that. We prefer a quality testing.
  19. 19. REFERENCES •http://www.artima.com/weblogs/viewpost.jsp? thread=126923 •http://en.wikipedia.org/wiki/Unit_testing •http://www.somkiat.cc/high-test-coverage-vs-quality/ •http://agileinaflash.blogspot.com/2009/02/first.html !• http://www.slideshare.net/up1/tdd-with-php
  20. 20. YOUR QUESTION? Any?

×