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.

B3b2139e4f2c0eca4efe2379fcebc1c5?s=128

Anna Filina

June 08, 2015
Tweet

Transcript

  1. 7.
  2. 8.

    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); } 8
  3. 10.

    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()); 10
  4. 11.
  5. 12.

    Test both cases $item = new CartItem("Skyrim", 30); $item->setQuantity(1); $cart->addItem($item);

    $this->assertEquals(15, $cart->getShipping()); $this->assertEquals(45, $cart->getTotal()); 12
  6. 13.

    Step 3: write code public function getTotal() { //... $taxes

    = $this->getApplicableTaxes(); foreach ($taxes as $tax) { $total += $subtotal * $tax->getPercent(); } //... } 13
  7. 14.

    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 14
  8. 15.

    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()); 15
  9. 18.

    Step 4: before (TDD) $import = new CatalogImport(); $products =

    $import->parseFromCsv('catalog.csv'); $this->assertInternalType('array', $products); $this->assertCount(2, $products); $this->assertArrayHasKey('name', $products[0]); $this->assertArrayHasKey('price', $products[0]); 18
  10. 19.

    Tips • How it’s not supposed to work. • Plan

    for exceptions. • Focus on realistic scenarios. 19
  11. 21.

    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 21
  12. 25.

    Zero times foreach ($products as $product) { $total = $product->price

    * $product->quantity; } if ($total > 0) { echo 'found'; } 25
  13. 26.

    Multiple times $total = 0; foreach ($products as $product) {

    $total = $product->price * $product->quantity; } if ($total > 0) { echo 'found'; } 26
  14. 27.

    Services • Development: PHP, JS, etc. • Fix problems: bugs,

    performance, etc. • Workshops: testing, Symfony, AngularJS • Advisor: testing strategy, architecture, etc. 27