This document discusses unit and integration testing. It begins by explaining the benefits of testing, such as reducing bugs and allowing safe refactoring. It then describes different types of tests like unit, integration, and database tests. The document focuses on unit testing, explaining how to write and organize unit tests using PHPUnit. It provides examples of test assertions and annotations. It also covers mocking and stubbing dependencies. Finally, it discusses challenges like testing code that relies on external components and provides strategies for database testing.
7. TESTS REDUCE BUGS
TESTS ARE GOOD DOCUMENTATION
TESTS ALLOW SAFE REFACTORING
8. TESTS REDUCE BUGS
TESTS ARE GOOD DOCUMENTATION
TESTS ALLOW SAFE REFACTORING
TESTS REDUCE THE COST OF CHANGE
9. TESTS REDUCE BUGS
TESTS ARE GOOD DOCUMENTATION
TESTS ALLOW SAFE REFACTORING
TESTS REDUCE THE COST OF CHANGE
TESTING FORCES YOU TO THINK
10. TESTS REDUCE BUGS
TESTS ARE GOOD DOCUMENTATION
TESTS ALLOW SAFE REFACTORING
TESTS REDUCE THE COST OF CHANGE
TESTING FORCES YOU TO THINK
TESTS REDUCE FEAR
11. A study conducted by Microsoft and
IBM showed that writing tests can
add 15% – 35% to development time
but reduce the number of bugs by
40% – 90%.
http://research.microsoft.com/en-us/groups/ese/nagappan_tdd.pdf
13. Black Box
White Box
Unit
Integration
Functional
System
Regression
Performance
Smoke
Canary
Usability
A/B
…
14. Black Box
White Box
Unit
Integration
Functional
System
Regression
Performance
Smoke
Canary
Usability
A/B
…
15. UNIT TESTS
The goal of unit testing is:
1. to isolate each part of the program, and
16. UNIT TESTS
The goal of unit testing is:
1. to isolate each part of the program, and
2. show that the individual parts are correct.
17. UNIT TESTS
The goal of unit testing is:
1. to isolate each part of the program, and
2. show that the individual parts are correct.
Unit tests have a very narrow and well-defined scope.
21. UNIT TESTS
Pro’s:
1. Fast
2. Simple to understand
3. Reliable
Con’s:
1. Large time investment
22. UNIT TESTS
Pro’s:
1. Fast
2. Simple to understand
3. Reliable
Con’s:
1. Large time investment
2. Requires maintenance
23. UNIT TESTS
A unit test should NOT:
1. Access the network
24. UNIT TESTS
A unit test should NOT:
1. Access the network
2. Hit a database
25. UNIT TESTS
A unit test should NOT:
1. Access the network
2. Hit a database
3. Use the file system
26. UNIT TESTS
A unit test should NOT:
1. Access the network
2. Hit a database
3. Use the file system
4. Call other non-trivial components
27. INTEGRATION TESTS
Test the inter-operation of multiple subsystems.
Pro’s:
1. Make sure nuts and bolts
fit together
28. INTEGRATION TESTS
Test the inter-operation of multiple subsystems.
Pro’s:
1. Make sure nuts and bolts
fit together
2. Test behaviour
and infrastructure
29. INTEGRATION TESTS
Test the inter-operation of multiple subsystems.
Pro’s:
1. Make sure nuts and bolts
fit together
2. Test behaviour
and infrastructure
3. (tested code) / test % is high
30. INTEGRATION TESTS
Test the inter-operation of multiple subsystems.
Pro’s:
1. Make sure nuts and bolts
fit together
2. Test behaviour
and infrastructure
3. (tested code) / test % is high
Con’s:
1. Hard to test all critical paths
31. INTEGRATION TESTS
Test the inter-operation of multiple subsystems.
Pro’s:
1. Make sure nuts and bolts
fit together
2. Test behaviour
and infrastructure
3. (tested code) / test % is high
Con’s:
1. Hard to test all critical paths
2. Harder to localise source
of errors
35. 1. Unit Testing Framework written in PHP by Sebastian Bergmann
2. De facto standard
3. Major Frameworks use it (Zend, Cake, Laravel, Symphony etc.)
38. ORGANISING TESTS
- The tests for class Foo are placed in a class FooTest
- Most of the time you will inherit from
PHPUnit_Framework_TestCase
PHPUnit_Extensions_Database_TestCase
- Tests are public methods named test*
- Inside the test methods, assertion methods such as
assertEquals() are used.
39. ORGANISING TESTS
Tests should mirror the code being tested.
SRC
SomeFolder
Baz.php
Foo.php
Bar.php
TEST
SomeFolder
FooTest.php
BarTest.php
BazTest.php
56. • db - Database Skel file and patches
• src - Project Code
• test/Output - Code coverage
• test/SportsBet - Projects Tests
• test/TestingCore - Uti l i ties
• tools - CI tools including ANT build
file