Automated testing with phpunit
1 / 32

Automated Testing with PHPUnit - PowerPoint PPT Presentation

  • Uploaded on
  • Presentation posted in: General

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.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

Download Presentation

Automated Testing with PHPUnit

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

Automated testing with phpunit

Automated Testing with PHPUnit

How do you know your code works

How do you know your code works?

Manual testing

Manual Testing

  • Type in a value

  • Submit the form

  • Check by eye

Manual testing1

Manual Testing

  • Tedious

  • Time Consuming

  • Error Prone

    • Did you test everything?

    • Consistently?

Automated testing

Automated Testing

Have the computer run your tests

Pro confidence

Pro: Confidence

Know that your code does what it needs to

Know that your code doesn’t break anything that used to work

Regression Testing

Con testing isn t free

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 testing

Unit Testing

Automated testing one unit at a time

What is a unit

What is a unit?

Module (procedural programming)

Class (OOP)

Breaking down unit tests

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

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

Automated testing with phpunit

XUnit Frameworks


Xunit frameworks provide

XUnit Frameworks Provide

  • Assertions

  • Calls Test Methods

  • Help Setup

  • Test Cases

  • Test Runners



  • 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

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 teardown

Help setUp and tearDown

  • Offers 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

Unit Test Life Cycle

Test case

Test Case

  • A class with test methods

  • Usually offers assert methods

  • Extended by the developer

  • class MyTestextends PHPUnit_Framework_ TestCase

  • {

  • }

Provide test runners

Provide Test Runners

Code that makes executing tests simple

Often a command-line tool

(useable by IDE)

Test first or test last

Test First or Test Last?

Test first

Test First

Test Driven Development (TDD)

Testing cycle

Testing Cycle

Find problems early

Find Problems Early

How should your code work?

What do the requirements mean?

Manage scope creep

Manage Scope Creep

Do The Simplest Thing That Could Possibly Work


Know when you’re done: All tests are green.

Test last

Test Last


Tests become biased for the code you already wrote

What don t you test

What don’t you test?

Don t test the language framework

Don’t Test the Language/Framework

  • Don’t test session session initiaiton

  • Or $_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

Don’t Test Methods Without Logic

  • class … {

  • private $foo;

  • public get_foo() {

  • return $this->foo;

  • }

  • }

What to test

What to Test



  • function … {

    • if ($foo > 3) {

    • // test whether execution goes in here

    • }

    • else {

    • // test whether execution goes in here

    • }

  • }

Invalid values

Invalid values

  • 0

  • Negative values

  • Empty strings

  • Very long strings (>500chars)

What s a good test

What’s a good test?

  • Repeatable

    • Use setUp() to make sure everything is right for your test

  • Independent

    • UsetearDown() to clean up

  • Thorough

    • It’s ok to have more than one test method in your case… in fact it’s often necessary!

  • Login