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. 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. You do not deserve that!
Unit testing can help you.
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. What help us to create quality unit test?
• PHP
• PHPUnit
• Dependency Injection
• Code coverage
• Belief
• and Conscious
7. With PHPUnit, you can
!
• Test your methods/functions
• Mock your classes
• Stub your classes
• Stub your functions
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. WHAT IS UNIT TESTING?
A software testing method to test individual unit
of source code — a method in class or a piece of
code
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. 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. 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. UNIT TESTING MEASUREMENT
•Line of code
•Branch
Code coverage DOES NOT guarantee that your application
does not contain bugs or has good quality.
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.
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.