2. Advantages to Automating Testing
â Faster to test
â Don't need to "click about"
â Don't need to login
â Don't need to have commented out test code
â Prevent recurring bugs
â Write a test for bug, run the test after every change
â Easier to test edge cases
â Less fear when making changes
â You may have forgotten how the code works
â You may inherit someone else's code
â Find bugs before your users
â This means you keep your customers happy, this
means more money!
3. Advantages to Automating Testing
â Better designed code, especially with TDD
â Unit tested code tends to be
â Less coupled
â Less complex
â Tests document how code should work
â Once you are good at unit testing, it's
probably faster
â Tests don't have to be amazing: "Imperfect
tests, run frequently, are much better than
perfect tests that are never written at all"
4. Disadvantages
â You have to suck it up and write tests to
really see the benefits
â Hard to retrofit tests
â You may have to refactor old code, or
â You may have to write a lot of test code for code
which is not easy to test
â Harder to write bad code
â You probably won't want to write code
without tests again
5. Goals When Writing Tests
â Tests should be
â Simple
â Don't have ifs or loops
â Easy to write
â Easy to read
â Easy to run
â Isolated
â But these ideas may conflict or be hard to
achieve
â Do your best
â Can improve tests over time
â Bad tests better than no tests
6. Common PHPUnit Methods
â $this->assertTrue($value, 'Value should
have been true')
â $this->assertFalse($value, 'Value should
have been false')
â $this->assertEquals($expected, $actual,
'The result is not what was expected')
â Try to write a message to indicate what
specifically is being asserted
â Easier to understand what's wrong instead of e.g.
"Failed asserting that false is true"
7. Common PHPUnit Patterns
â Use setUp() method to instantiate the class
you are testing
â This works well if you don't have lots of constructor
parameters
â Use data providers to test lots of different
inputs
â Tests shouldn't have duplicate code either
â Use tearDown() method if you need to clean
up external resources after each test is run
â Files
â Database
8. Hard to Test Things
â Static calls to other classes
â You cannot mock the dependency
â Singletons
â Constructor is private or protected
â Uses static call to get instance
â Cannot guarantee there are no side effects when
running multiple tests
â Private methods
â Usually you indirectly test these when you test public
methods
â Can use reflection if you really want, but this
probably means you should refactor
9. Hard to Test Things
â Hard coded dependencies
â Such as when a class instantiates another class
â Magic methods
â __call
â __get
â __set
10. How to Write Testable Code
â Use dependency injection
â This means you don't instantiate classes unless
you're in a factory
â Keep methods relatively small
â Avoid having lots of private methods
â Write classes which do one thing (single
responsibility principle)
11. Go go go!
â http://www.phpunit.
de/manual/current/en/index.html