automated testing with phpunit

32
Automated Testing with PHPUnit

Upload: hide

Post on 24-Feb-2016

93 views

Category:

Documents


0 download

DESCRIPTION

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 Testing. Have the computer run your tests. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Automated  Testing with  PHPUnit

Automated Testing with PHPUnit

Page 2: Automated  Testing with  PHPUnit

How do you know your code works?

Page 3: Automated  Testing with  PHPUnit

Manual Testing• Type in a value• Submit the form• Check by eye

Page 4: Automated  Testing with  PHPUnit

Manual Testing• Tedious• Time Consuming• Error Prone• Did you test everything?• Consistently?

Page 5: Automated  Testing with  PHPUnit

Automated TestingHave the computer run your tests

Page 6: Automated  Testing with  PHPUnit

Pro: ConfidenceKnow that your code does what it needs toKnow that your code doesn’t break anything that used to work

Regression Testing

Page 7: Automated  Testing with  PHPUnit

Con: Testing isn’t free• Writing tests takes time• Response: So does manual testing

• Mo Code Mo Problems• Response: It’s a worthwhile investment

Page 8: Automated  Testing with  PHPUnit

Unit TestingAutomated testing one unit at a time

Page 9: Automated  Testing with  PHPUnit

What is a unit?Module (procedural programming)Class (OOP)

Page 10: Automated  Testing with  PHPUnit

Breaking Down Unit Tests• Test Suite composed of• Test Case Classes each of which

have• Test Methods which make 1 or more• Assertions

Page 11: Automated  Testing with  PHPUnit

A Unit Test Asserts That A Unit Of Code Performs To A Specification

Page 12: Automated  Testing with  PHPUnit

HelpXUnit Frameworks

Page 13: Automated  Testing with  PHPUnit

XUnit Frameworks Provide• Assertions• Calls Test Methods• Help Setup• Test Cases• Test Runners

Page 14: Automated  Testing with  PHPUnit

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()

Page 15: Automated  Testing with  PHPUnit

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…() { … }

}

Page 16: Automated  Testing with  PHPUnit

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() {…}

}

Page 17: Automated  Testing with  PHPUnit

Unit Test Life Cycle

TestCase:: setUp()

TestCase:: test*()

TestCase:: tearDown()

Page 18: Automated  Testing with  PHPUnit

Test Case• A class with test

methods• Usually offers

assert methods• Extended by the

developer

class MyTest extends PHPUnit_Framework_ TestCase {…}

Page 19: Automated  Testing with  PHPUnit

Provide Test RunnersCode that makes executing tests simpleOften a command-line tool

(useable by IDE)

Page 20: Automated  Testing with  PHPUnit

Test First or Test Last?

Page 21: Automated  Testing with  PHPUnit

Test FirstTest Driven Development (TDD)

Page 22: Automated  Testing with  PHPUnit

Testing Cycle

Write Test

Confirm Test Fails

Make Test Pass

Ensure All Tests

Pass

Page 23: Automated  Testing with  PHPUnit

Find Problems EarlyHow should your code work?What do the requirements mean?

Page 24: Automated  Testing with  PHPUnit

Manage Scope CreepDo The Simplest Thing That Could Possibly WorkYAGNIKnow when you’re done: All tests are green.

Page 25: Automated  Testing with  PHPUnit

Test LastProblematicTests become biased for the code you already wrote

Page 26: Automated  Testing with  PHPUnit

What don’t you test?

Page 27: Automated  Testing with  PHPUnit

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!

Page 28: Automated  Testing with  PHPUnit

Don’t Test Methods Without Logic

class … { private $foo; public get_foo() { return $this->foo; }}

Page 29: Automated  Testing with  PHPUnit

What to Test

Page 30: Automated  Testing with  PHPUnit

Boundariesfunction … {

if ($foo > 3) { // test whether execution goes in here}else { // test whether execution goes in here}

}

Page 31: Automated  Testing with  PHPUnit

Invalid values• 0• Negative values• Empty strings• Very long strings (>500chars)

Page 32: Automated  Testing with  PHPUnit

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!