Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Unit Testing by Example

Unit Testing by Example

Everyone tells you that you need to write tests. You tried it, but your tests ended up not very useful and consumed valuable time. What to test? What conditions to write? Through realistic examples, this presentation will take you from var_dump() and ease you into the testing business. All this without losing sight of the tight deadlines. You will come out of this presentation with a renewed interest in writing unit tests.

Anna Filina
PRO

February 26, 2014
Tweet

More Decks by Anna Filina

Other Decks in Technology

Transcript

  1. FooLab
    http://foolab.ca
    @foolabca
    Unit Testing
    by Example
    ConFoo - February 26, 2014

    View Slide

  2. FooLab
    Objectives
    • Make testing enjoyable.
    • Make it useful.
    • Reduce pre-release stress.
    2

    View Slide

  3. FooLab
    Anna Filina
    3
    • I help projects ship on time.
    • I train and mentor developers.
    • I code and optimize.

    View Slide

  4. FooLab
    You don’t need
    100% coverage

    View Slide

  5. FooLab
    You don’t become an
    expert overnight

    View Slide

  6. FooLab
    Step 1: found bug
    $item = new CartItem("Skyrim", 30);
    $item->setQuantity(0.1);
    $this->assertEquals(1, $item->getQuantity());
    ===
    public function setQuantity($qty)
    {
    $this->qty = ceil($qty);
    }
    6

    View Slide

  7. FooLab
    Step 2: improve code
    $item = new CartItem("Skyrim", 30);
    $item->setQuantity(2);
    $cart->addItem($item);
    $this->assertEquals(0, $cart->getShipping());
    $this->assertEquals(60, $cart->getTotal());
    7

    View Slide

  8. FooLab
    Step 2: improve code
    public function getShipping()
    {
    if ($this->getSubtotal() >= 40) {
    return 0;
    }
    else {
    return 15;
    }
    }
    8

    View Slide

  9. FooLab
    Test both cases
    $item = new CartItem("Skyrim", 30);
    $item->setQuantity(1);
    $cart->addItem($item);
    $this->assertEquals(15, $cart->getShipping());
    $this->assertEquals(45, $cart->getTotal());
    9

    View Slide

  10. FooLab
    Step 3: write code
    public function getTotal()
    {
    ...
    $taxes = $this->getApplicableTaxes();
    foreach ($taxes as $tax)
    {
    $total += $subtotal * $tax->getPercent();
    }
    ...
    }
    10

    View Slide

  11. FooLab
    Step 3: write code
    array (size=2)
    0 =>
    array (size=2)
    'name' => string 'GST' (length=3)
    'percent' => float 0.5
    1 =>
    array (size=2)
    'name' => string 'QST' (length=3)
    'percent' => float 0.9975
    11

    View Slide

  12. FooLab
    Step 3: write code
    $cart = new Cart();
    $taxes = $cart->getApplicableTaxes();
    $this->assertInternalType('array', $taxes);
    $this->assertCount(2, $taxes);
    $gst = $taxes[0];
    $this->assertEquals(0.5, $gst->getPercent());
    12

    View Slide

  13. FooLab
    Advantages
    • More tests.
    • Easy to write.
    • Never fall too far.
    13

    View Slide

  14. FooLab
    Step 4: before (TDD)
    $import = new CatalogImport();
    $products = $import->parseFromCsv('test_catalog.csv');
    $this->assertInternalType('array', $products);
    $this->assertCount(2, $products);
    $this->assertArrayHasKey('name', products[0]);
    $this->assertArrayHasKey('price', products[0]);
    14

    View Slide

  15. FooLab
    Tips
    • How it’s not supposed to
    work.
    • Plan for exceptions.
    • Focus on realistic
    scenarios.
    15

    View Slide

  16. FooLab
    Recap
    • Testing takes practice
    • Write tests when you see
    a bug
    • Write tests when you
    improve code
    • Write tests as you write
    new code
    • Write tests before you
    write code
    • Test unexpected
    scenarios
    16

    View Slide