Automated Testing with PHPUnit
description
Transcript of Automated Testing with PHPUnit
Automated Testing with PHPUnit
How do you know your code works?
Manual Testing• Type in a value• Submit the form• Check by eye
Manual Testing• Tedious• Time Consuming• Error Prone• Did you test everything?• Consistently?
Automated TestingHave the computer run your tests
Pro: ConfidenceKnow that your code does what it needs toKnow that your code doesn’t break anything that used to work
Regression Testing
Con: Testing isn’t free• Writing tests takes time• Response: So does manual testing
• Mo Code Mo Problems• Response: It’s a worthwhile investment
Unit TestingAutomated testing one unit at a time
What is a unit?Module (procedural programming)Class (OOP)
Breaking Down Unit Tests• Test Suite composed of• Test Case Classes each of which
have• Test Methods which make 1 or more• Assertions
A Unit Test Asserts That A Unit Of Code Performs To A Specification
HelpXUnit Frameworks
XUnit Frameworks Provide• Assertions• Calls Test Methods• Help Setup• Test Cases• Test Runners
Assertions• Code to verify that
expected and actual values match
• Often methods starting with “assert…()” or “refute…()”
• Used by the developer
$this->assertTrue()
$this-> assertFalse()
$this-> assertEquals()
Calls Test Methods• Knows to call your
code which tests a particular condition, method, or other “sub unit”
• Methods usually begin with “Test…()”
• Written by the developer
class … {public function test…() { … }
}
Help setUp and tearDownOffers special functions that get called before and after each test.Written by the developer
class … {public
function setUp() {…}public function tearDown() {…}
}
Unit Test Life Cycle
TestCase:: setUp()
TestCase:: test*()
TestCase:: tearDown()
Test Case• A class with test
methods• Usually offers
assert methods• Extended by the
developer
class MyTest extends PHPUnit_Framework_ TestCase {…}
Provide Test RunnersCode that makes executing tests simpleOften a command-line tool
(useable by IDE)
Test First or Test Last?
Test FirstTest Driven Development (TDD)
Testing Cycle
Write Test
Confirm Test Fails
Make Test Pass
Ensure All Tests
Pass
Find Problems EarlyHow should your code work?What do the requirements mean?
Manage Scope CreepDo The Simplest Thing That Could Possibly WorkYAGNIKnow when you’re done: All tests are green.
Test LastProblematicTests become biased for the code you already wrote
What don’t you test?
Don’t Test the Language/Framework
Don’t test session session initiaitonOr $_POST/$_GET/etc.Or anything provided by PHP (and/or your framework,
$_POST[“loggedIn”] = true;$this-> assertTrue($_POST [“loggedIn”])# Wrong!
Don’t Test Methods Without Logic
class … { private $foo; public get_foo() { return $this->foo; }}
What to Test
Boundariesfunction … {
if ($foo > 3) { // test whether execution goes in here}else { // test whether execution goes in here}
}
Invalid values• 0• Negative values• Empty strings• Very long strings (>500chars)
What’s a good test?• Repeatable• Use setUp() to make sure everything is
right for your test• Independent• Use tearDown() to clean up
• Thorough• It’s ok to have more than one test
method in your case… in fact it’s often necessary!