Unit testing like a pirate #wceu 2013

UNIT TESTING
likea
cPirate
WordCamp Europe 2013
with
pirate dunbar
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
YES,
I am a Pirate.
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nAPP Platform
WordPress as a
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nAPP Platform
WordPress as a
Video chat marketplace
for experts to monetize
their know-how.
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nAPP Platform
WordPress as a
“I think WordPress is going in a
direction that isn’t well framed or
described by any existing alternatives
or historical platforms.”
— Matt Mullenweghttp://jakegoldman.me/2013/09/wordpress-app-platform/
Saturday, October 5, 13
“Plugins represent the
heartbeat of
WordPress.”
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
“The result is that a lot of the
plugins are written in poor code
and turn out to be poorly
compatible with other plugins.”
— Yoast
http://yoast.com/plugin-future/
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Manual Testing
Saturday, October 5, 13
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Problems of manual testing
1. Ad-hoc ☹
Saturday, October 5, 13
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Problems of manual testing
1. Ad-hoc ☹
2. Slow & Error Prone ☹
Saturday, October 5, 13
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Problems of manual testing
1. Ad-hoc ☹
2. Slow & Error Prone ☹
3. High risk that I missed something ☹
Saturday, October 5, 13
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what we need
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what we need
• Fast & Instant feedback
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what we need
• Fast & Instant feedback
• Reusable and organized
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what we need
• Fast & Instant feedback
• Reusable and organized
• Verify that I didn’t break anything.
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
Unit Testing Series
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
Unit Testing Series
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
1. Unit Testing like a Pirate
2. Buccaneering the high seas with PHPUnit
3. Mo’ plugins mo’ problems
4. Steering clear of wreckages and skulls
5. Avast! TDD for sailors and salty dogs
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is unit testing
“An automated piece of code that
invokes your application code to
check a single assumption.”
— Pirate Dunbar
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is unit testing
“Separating
the application
design &
implementation
process.”
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
“A command line
tool that runs
unit tests &
reports their
results.”
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
$>phpunit
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
Time: 1 sec, Memory: 8.75Mb
OK (200 tests, 355 assertions)
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
There was 1 failure:
1) WPSkeletonPluginTestAutoloadTest::testThatItsTestingTime
What time is it? - it's testing time! :D
/Users/ptah/Sites/wp.t/public/content/mu-plugins/wp-
skeleton-plugin/tests/WPSkeletonPluginTest/AutoloadTest.php:
9
FAILURES!
Tests: 2, Assertions: 1, Failures: 1.
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
Test Case
a set of conditions or
variables that you set
up in order to assert an
expected outcome.
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
Test SUITE
A collection of test cases.
Saturday, October 5, 13
<?php
class SubscriptionTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testHasAccessWithPassDueCustomerBlocksAccess()
{
// AAA
}
public function testAddSubscriptionWithInvalidCustIdReturnsWPError()
{
// AAA
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Assertions
“a way of explicitly checking the
assumptions that your code
makes”
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
n• $this->assertTrue();
• $this->assertEquals();
• $this->assertContains();
• $this->assertGreaterThan();
• $this->assertNotNull();
• $this->assertFalse();
• $this->assertNotEquals();
• $this->assertContainsOnly();
• $this->assertLessThan();
• $this->assertType();
Assertions Appendix:
http://phpunit.de/manual/3.7/en/appendixes.assertions.html
common Assertions
Saturday, October 5, 13
“How do I actually write
out the test cases?”
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
http://www.flickr.com/photos/mutsmuts/4695658106
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. a
2. A
3. A
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. A
2. A
3. Assert(check for the expected value)
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. A
2. Act(call the method/trigger the action)
3. Assert(check for the expected value)
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. arrange (the context)
2. Act(call the method/trigger the action)
3. Assert(check for the expected value)
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
int Calculator::add(int $a, int $b);
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
// Act
// Assert
$this->assertEquals(3, $result);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
// Act
$result = $calculator->add($a, $b);
// Assert
$this->assertEquals(3, $result);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
$calculator = new Calculator();
$a = 1;
$b = 2;
// Act
$result = $calculator->add($a, $b);
// Assert
$this->assertEquals(3, $result);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPositiveIntegers()
{
// Arrange
$calculator = new Calculator();
$a = 1;
$b = 2;
// Act
$result = $calculator->add($a, $b);
// Assert
$this->assertEquals(3, $result);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Time: 148 ms, Memory: 2.75Mb
OK (1 test, 1 assertions)
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
bool Cache::set(string $key);
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
// Act
// Assert
$this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) );
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
// Act
$cache->add(‘user_ids’, [ 1, 2, 3 ]);
// Assert
$this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) );
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
$cache = new LiveNinjaStorageCache();
// Act
$cache->add(‘user_ids’, [ 1, 2, 3 ]);
// Assert
$this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) );
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPersistsToCache()
{
// Arrange
$cache = new LiveNinjaStorageCache();
// Act
$cache->add(‘user_ids’, [ 1, 2, 3 ]);
// Assert
$this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) );
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Time: 125 ms, Memory: 2.35Mb
OK (1 test, 1 assertions)
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
object|WP_Error UserService::persist(User $user);
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
// Act
// Assert
$this->assertInstanceOf(‘WP_Error’, $user);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
// Act
$user = $service->persist($inCompleteUserObject);
// Assert
$this->assertInstanceOf(‘WP_Error’, $user);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
$service = new LiveNinjaUserService;
$inCompleteUserObject = new LiveNinjaUserEntity(...);
// Act
$user = $service->persist($inCompleteUserObject);
// Assert
$this->assertInstanceOf(‘WP_Error’, $user);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields()
{
// Arrange
$service = new LiveNinjaUserService;
$inCompleteUserObject = new LiveNinjaUserEntity(...);
// Act
$user = $service->persist($inCompleteUserObject);
// Assert
$this->assertInstanceOf(‘WP_Error’, $user);
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Time: 85 ms, Memory: 1.25Mb
OK (1 test, 1 assertions)
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
array|WP_Error UserService::get_ninjas(array $args);
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
// Act
// Assert
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
// Act
// Assert
$this->assertContains($expected, $service->getLastResultAsSQL());
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
// Act
$service->get_ninjas([‘plan’ => ‘blackbelt’]);
// Assert
$this->assertContains($expected, $service->getLastResultAsSQL());
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
$expected = “SELECT * FROM ninjas WHERE ...”;
$service = new LiveNinjaUserService;
// Act
$service->get_ninjas([‘plan’ => ‘blackbelt’]);
// Assert
$this->assertSame($expected, $service->getLastResultAsSQL());
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Saturday, October 5, 13
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackBeltSubscriptionQuery()
{
// Arrange
$expected = “SELECT * FROM ninjas WHERE ...”;
$service = new LiveNinjaUserService;
// Act
$service->get_ninjas([‘plan’ => ‘blackbelt’]);
// Assert
$this->assertContains($expected, $service->getLastResultAsSQL());
}
}
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Time: 101 ms, Memory: 1.55Mb
OK (1 test, 1 assertions)
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nWAYS TO TEST CODE
• test if/else conditionals
• test all cases in switch
• verify loops contain proper
data.
• Check for expected state
• Check return value type
• Pass unexpected data
• Verify correct amount
• assertContains for SQL
Queries
• Verify that third party
method was called.
• $this->assertType();
• etc...
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nWAYS TO kickstart your brain
• write out list of requirements for
each feature
• do the UI first
Saturday, October 5, 13
cwp skeleton plugin
https://github.com/ptahdunbar/wp-skeleton-plugin/
Saturday, October 5, 13
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Thank you!
Slides - http://ptahdunbar.com/
cPirate Dunbar
Saturday, October 5, 13
1 of 65

Recommended

Storyplayer by
StoryplayerStoryplayer
StoryplayerStuart Herbert
3.5K views83 slides
CPANci: Continuous Integration for CPAN by
CPANci: Continuous Integration for CPANCPANci: Continuous Integration for CPAN
CPANci: Continuous Integration for CPANMike Friedman
2.5K views135 slides
Drupal and Cloud Containers by
Drupal and Cloud ContainersDrupal and Cloud Containers
Drupal and Cloud ContainersJosh Koenig
787 views49 slides
Infrastructure as Data - PuppetConf 2013 by
Infrastructure as Data - PuppetConf 2013Infrastructure as Data - PuppetConf 2013
Infrastructure as Data - PuppetConf 2013Puppet
1.3K views54 slides
Become Master of Your Own Universe - DIBI 2013 by
Become Master of Your Own Universe - DIBI 2013Become Master of Your Own Universe - DIBI 2013
Become Master of Your Own Universe - DIBI 2013Phil Sturgeon
3.1K views39 slides
Unit Test + Functional Programming = Love by
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
16.9K views68 slides

More Related Content

Similar to Unit testing like a pirate #wceu 2013

Building scalable applications while scaling your infrastructure by rhommel l... by
Building scalable applications while scaling your infrastructure by rhommel l...Building scalable applications while scaling your infrastructure by rhommel l...
Building scalable applications while scaling your infrastructure by rhommel l...Puppet
1K views51 slides
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013 by
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013Puppet
3.4K views53 slides
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid... by
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...Pablo Godel
8.7K views132 slides
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013 by
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013Puppet
8.8K views40 slides
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013 by
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013cordoval
1.6K views58 slides
Ab(Using) the MetaCPAN API for Fun and Profit v2013 by
Ab(Using) the MetaCPAN API for Fun and Profit v2013Ab(Using) the MetaCPAN API for Fun and Profit v2013
Ab(Using) the MetaCPAN API for Fun and Profit v2013Olaf Alders
5.6K views68 slides

Similar to Unit testing like a pirate #wceu 2013(20)

Building scalable applications while scaling your infrastructure by rhommel l... by Puppet
Building scalable applications while scaling your infrastructure by rhommel l...Building scalable applications while scaling your infrastructure by rhommel l...
Building scalable applications while scaling your infrastructure by rhommel l...
Puppet1K views
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013 by Puppet
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013
Using Orchestration in Puppet Enterprise 3 - PuppetConf 2013
Puppet3.4K views
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid... by Pablo Godel
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...
PHP Conference Argentina 2013 - Independizate de tu departamento IT - Habilid...
Pablo Godel8.7K views
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013 by Puppet
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013
What Is This Continuous Delivery Thing Anyway? - PuppetConf 2013
Puppet8.8K views
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013 by cordoval
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013
Specking Interactors with PHPSpec and YOLO (DDD) at PHPConference Argentina 2013
cordoval1.6K views
Ab(Using) the MetaCPAN API for Fun and Profit v2013 by Olaf Alders
Ab(Using) the MetaCPAN API for Fun and Profit v2013Ab(Using) the MetaCPAN API for Fun and Profit v2013
Ab(Using) the MetaCPAN API for Fun and Profit v2013
Olaf Alders5.6K views
Automated Unit Testing by Mike Lively
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
Mike Lively4.7K views
Node Tools For Your Grails Toolbox - Gr8Conf 2013 by zanthrash
Node Tools For Your Grails Toolbox - Gr8Conf 2013Node Tools For Your Grails Toolbox - Gr8Conf 2013
Node Tools For Your Grails Toolbox - Gr8Conf 2013
zanthrash2.9K views
Keeping it small - Getting to know the Slim PHP micro framework by Jeremy Kendall
Keeping it small - Getting to know the Slim PHP micro frameworkKeeping it small - Getting to know the Slim PHP micro framework
Keeping it small - Getting to know the Slim PHP micro framework
Jeremy Kendall9.6K views
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th... by Puppet
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Releasing Puppet: Automating Packaging for Many Platforms or 'Make all the th...
Puppet6.4K views
Cooking an Omelette with Chef by ctaintor
Cooking an Omelette with ChefCooking an Omelette with Chef
Cooking an Omelette with Chef
ctaintor1.1K views
Testing Drupal with Ghosts and Gherkin by Phase2
Testing Drupal  with Ghosts and GherkinTesting Drupal  with Ghosts and Gherkin
Testing Drupal with Ghosts and Gherkin
Phase233.1K views
Apache TomEE, Java EE 6 Web Profile on Tomcat - David Blevins by jaxconf
Apache TomEE, Java EE 6 Web Profile on Tomcat - David BlevinsApache TomEE, Java EE 6 Web Profile on Tomcat - David Blevins
Apache TomEE, Java EE 6 Web Profile on Tomcat - David Blevins
jaxconf480 views
Node Security Project - LXJS 2013 by Adam Baldwin
Node Security Project - LXJS 2013Node Security Project - LXJS 2013
Node Security Project - LXJS 2013
Adam Baldwin2.4K views
Forging Great Modules: Standards, Tools and Patterns - PuppetConf 2013 by Puppet
Forging Great Modules: Standards, Tools and Patterns - PuppetConf 2013Forging Great Modules: Standards, Tools and Patterns - PuppetConf 2013
Forging Great Modules: Standards, Tools and Patterns - PuppetConf 2013
Puppet5K views
Scaling PHP to 40 Million Uniques by Jonathan Klein
Scaling PHP to 40 Million UniquesScaling PHP to 40 Million Uniques
Scaling PHP to 40 Million Uniques
Jonathan Klein7.3K views
Automated Testing in WordPress, Really?! by Ptah Dunbar
Automated Testing in WordPress, Really?!Automated Testing in WordPress, Really?!
Automated Testing in WordPress, Really?!
Ptah Dunbar22.6K views
A Big Look at MiniTest by Mark
A Big Look at MiniTestA Big Look at MiniTest
A Big Look at MiniTest
Mark 1.6K views
Corpus collapsum: Partition tolerance of Galera put to test by Raghavendra Prabhu
Corpus collapsum: Partition tolerance of Galera put to testCorpus collapsum: Partition tolerance of Galera put to test
Corpus collapsum: Partition tolerance of Galera put to test
Raghavendra Prabhu788 views
Teaching Programming Online by Pamela Fox
Teaching Programming OnlineTeaching Programming Online
Teaching Programming Online
Pamela Fox1.4K views

Recently uploaded

"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad... by
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad..."Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...Fwdays
40 views30 slides
Future of Learning - Yap Aye Wee.pdf by
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdfNUS-ISS
38 views11 slides
The Importance of Cybersecurity for Digital Transformation by
The Importance of Cybersecurity for Digital TransformationThe Importance of Cybersecurity for Digital Transformation
The Importance of Cybersecurity for Digital TransformationNUS-ISS
25 views26 slides
The Research Portal of Catalonia: Growing more (information) & more (services) by
The Research Portal of Catalonia: Growing more (information) & more (services)The Research Portal of Catalonia: Growing more (information) & more (services)
The Research Portal of Catalonia: Growing more (information) & more (services)CSUC - Consorci de Serveis Universitaris de Catalunya
66 views25 slides
MemVerge: Past Present and Future of CXL by
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXLCXL Forum
110 views26 slides
Business Analyst Series 2023 - Week 3 Session 5 by
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5DianaGray10
165 views20 slides

Recently uploaded(20)

"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad... by Fwdays
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad..."Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
"Ukrainian Mobile Banking Scaling in Practice. From 0 to 100 and beyond", Vad...
Fwdays40 views
Future of Learning - Yap Aye Wee.pdf by NUS-ISS
Future of Learning - Yap Aye Wee.pdfFuture of Learning - Yap Aye Wee.pdf
Future of Learning - Yap Aye Wee.pdf
NUS-ISS38 views
The Importance of Cybersecurity for Digital Transformation by NUS-ISS
The Importance of Cybersecurity for Digital TransformationThe Importance of Cybersecurity for Digital Transformation
The Importance of Cybersecurity for Digital Transformation
NUS-ISS25 views
MemVerge: Past Present and Future of CXL by CXL Forum
MemVerge: Past Present and Future of CXLMemVerge: Past Present and Future of CXL
MemVerge: Past Present and Future of CXL
CXL Forum110 views
Business Analyst Series 2023 - Week 3 Session 5 by DianaGray10
Business Analyst Series 2023 -  Week 3 Session 5Business Analyst Series 2023 -  Week 3 Session 5
Business Analyst Series 2023 - Week 3 Session 5
DianaGray10165 views
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa... by The Digital Insurer
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...
Webinar : Competing for tomorrow’s leaders – How MENA insurers can win the wa...
AI: mind, matter, meaning, metaphors, being, becoming, life values by Twain Liu 刘秋艳
AI: mind, matter, meaning, metaphors, being, becoming, life valuesAI: mind, matter, meaning, metaphors, being, becoming, life values
AI: mind, matter, meaning, metaphors, being, becoming, life values
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM by CXL Forum
Samsung: CMM-H Tiered Memory Solution with Built-in DRAMSamsung: CMM-H Tiered Memory Solution with Built-in DRAM
Samsung: CMM-H Tiered Memory Solution with Built-in DRAM
CXL Forum105 views
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ... by Fwdays
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ..."Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
"Quality Assurance: Achieving Excellence in startup without a Dedicated QA", ...
Fwdays33 views
Transcript: The Details of Description Techniques tips and tangents on altern... by BookNet Canada
Transcript: The Details of Description Techniques tips and tangents on altern...Transcript: The Details of Description Techniques tips and tangents on altern...
Transcript: The Details of Description Techniques tips and tangents on altern...
BookNet Canada119 views
Five Things You SHOULD Know About Postman by Postman
Five Things You SHOULD Know About PostmanFive Things You SHOULD Know About Postman
Five Things You SHOULD Know About Postman
Postman25 views
TE Connectivity: Card Edge Interconnects by CXL Forum
TE Connectivity: Card Edge InterconnectsTE Connectivity: Card Edge Interconnects
TE Connectivity: Card Edge Interconnects
CXL Forum96 views
Liqid: Composable CXL Preview by CXL Forum
Liqid: Composable CXL PreviewLiqid: Composable CXL Preview
Liqid: Composable CXL Preview
CXL Forum121 views
Spesifikasi Lengkap ASUS Vivobook Go 14 by Dot Semarang
Spesifikasi Lengkap ASUS Vivobook Go 14Spesifikasi Lengkap ASUS Vivobook Go 14
Spesifikasi Lengkap ASUS Vivobook Go 14
Dot Semarang35 views
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi by Fwdays
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
"AI Startup Growth from Idea to 1M ARR", Oleksandr Uspenskyi
Fwdays26 views
JCon Live 2023 - Lice coding some integration problems by Bernd Ruecker
JCon Live 2023 - Lice coding some integration problemsJCon Live 2023 - Lice coding some integration problems
JCon Live 2023 - Lice coding some integration problems
Bernd Ruecker67 views
Future of Learning - Khoong Chan Meng by NUS-ISS
Future of Learning - Khoong Chan MengFuture of Learning - Khoong Chan Meng
Future of Learning - Khoong Chan Meng
NUS-ISS31 views
PharoJS - Zürich Smalltalk Group Meetup November 2023 by Noury Bouraqadi
PharoJS - Zürich Smalltalk Group Meetup November 2023PharoJS - Zürich Smalltalk Group Meetup November 2023
PharoJS - Zürich Smalltalk Group Meetup November 2023
Noury Bouraqadi113 views

Unit testing like a pirate #wceu 2013

  • 1. UNIT TESTING likea cPirate WordCamp Europe 2013 with pirate dunbar Saturday, October 5, 13
  • 2. UNIT TESTING like A PIRATE — @ptahdunbar #wceu YES, I am a Pirate. Saturday, October 5, 13
  • 3. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nAPP Platform WordPress as a Saturday, October 5, 13
  • 4. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nAPP Platform WordPress as a Video chat marketplace for experts to monetize their know-how. Saturday, October 5, 13
  • 5. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nAPP Platform WordPress as a “I think WordPress is going in a direction that isn’t well framed or described by any existing alternatives or historical platforms.” — Matt Mullenweghttp://jakegoldman.me/2013/09/wordpress-app-platform/ Saturday, October 5, 13
  • 6. “Plugins represent the heartbeat of WordPress.” UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 7. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu “The result is that a lot of the plugins are written in poor code and turn out to be poorly compatible with other plugins.” — Yoast http://yoast.com/plugin-future/ Saturday, October 5, 13
  • 8. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Manual Testing Saturday, October 5, 13
  • 9. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Problems of manual testing 1. Ad-hoc ☹ Saturday, October 5, 13
  • 10. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Problems of manual testing 1. Ad-hoc ☹ 2. Slow & Error Prone ☹ Saturday, October 5, 13
  • 11. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Problems of manual testing 1. Ad-hoc ☹ 2. Slow & Error Prone ☹ 3. High risk that I missed something ☹ Saturday, October 5, 13
  • 12. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 13. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need Saturday, October 5, 13
  • 14. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need • Fast & Instant feedback Saturday, October 5, 13
  • 15. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need • Fast & Instant feedback • Reusable and organized Saturday, October 5, 13
  • 16. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need • Fast & Instant feedback • Reusable and organized • Verify that I didn’t break anything. Saturday, October 5, 13
  • 17. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 18. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 19. Unit Testing Series UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 20. Unit Testing Series UNIT TESTING like A PIRATE — @ptahdunbar #wceu 1. Unit Testing like a Pirate 2. Buccaneering the high seas with PHPUnit 3. Mo’ plugins mo’ problems 4. Steering clear of wreckages and skulls 5. Avast! TDD for sailors and salty dogs Saturday, October 5, 13
  • 21. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is unit testing “An automated piece of code that invokes your application code to check a single assumption.” — Pirate Dunbar Saturday, October 5, 13
  • 22. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is unit testing “Separating the application design & implementation process.” Saturday, October 5, 13
  • 23. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT “A command line tool that runs unit tests & reports their results.” Saturday, October 5, 13
  • 24. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT $>phpunit Saturday, October 5, 13
  • 25. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT Time: 1 sec, Memory: 8.75Mb OK (200 tests, 355 assertions) Saturday, October 5, 13
  • 26. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT There was 1 failure: 1) WPSkeletonPluginTestAutoloadTest::testThatItsTestingTime What time is it? - it's testing time! :D /Users/ptah/Sites/wp.t/public/content/mu-plugins/wp- skeleton-plugin/tests/WPSkeletonPluginTest/AutoloadTest.php: 9 FAILURES! Tests: 2, Assertions: 1, Failures: 1. Saturday, October 5, 13
  • 27. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT Test Case a set of conditions or variables that you set up in order to assert an expected outcome. Saturday, October 5, 13
  • 28. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT Test SUITE A collection of test cases. Saturday, October 5, 13
  • 29. <?php class SubscriptionTest extends PHPUnit_Framework_TestCase { // test cases ... public function testHasAccessWithPassDueCustomerBlocksAccess() { // AAA } public function testAddSubscriptionWithInvalidCustIdReturnsWPError() { // AAA } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 30. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Assertions “a way of explicitly checking the assumptions that your code makes” Saturday, October 5, 13
  • 31. UNIT TESTING like A PIRATE — @ptahdunbar #wceu n• $this->assertTrue(); • $this->assertEquals(); • $this->assertContains(); • $this->assertGreaterThan(); • $this->assertNotNull(); • $this->assertFalse(); • $this->assertNotEquals(); • $this->assertContainsOnly(); • $this->assertLessThan(); • $this->assertType(); Assertions Appendix: http://phpunit.de/manual/3.7/en/appendixes.assertions.html common Assertions Saturday, October 5, 13
  • 32. “How do I actually write out the test cases?” UNIT TESTING like A PIRATE — @ptahdunbar #wceu http://www.flickr.com/photos/mutsmuts/4695658106 Saturday, October 5, 13
  • 33. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case Saturday, October 5, 13
  • 34. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case 1. a 2. A 3. A Saturday, October 5, 13
  • 35. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case 1. A 2. A 3. Assert(check for the expected value) Saturday, October 5, 13
  • 36. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case 1. A 2. Act(call the method/trigger the action) 3. Assert(check for the expected value) Saturday, October 5, 13
  • 37. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case 1. arrange (the context) 2. Act(call the method/trigger the action) 3. Assert(check for the expected value) Saturday, October 5, 13
  • 38. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu int Calculator::add(int $a, int $b); Saturday, October 5, 13
  • 39. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 40. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange // Act // Assert $this->assertEquals(3, $result); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 41. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange // Act $result = $calculator->add($a, $b); // Assert $this->assertEquals(3, $result); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 42. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange $calculator = new Calculator(); $a = 1; $b = 2; // Act $result = $calculator->add($a, $b); // Assert $this->assertEquals(3, $result); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 43. <?php class CalculatorTest extends PHPUnit_Framework_TestCase { // test cases ... public function testAddReturnsSumOfTwoPositiveIntegers() { // Arrange $calculator = new Calculator(); $a = 1; $b = 2; // Act $result = $calculator->add($a, $b); // Assert $this->assertEquals(3, $result); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Time: 148 ms, Memory: 2.75Mb OK (1 test, 1 assertions) Saturday, October 5, 13
  • 44. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu bool Cache::set(string $key); Saturday, October 5, 13
  • 45. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 46. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange // Act // Assert $this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) ); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 47. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange // Act $cache->add(‘user_ids’, [ 1, 2, 3 ]); // Assert $this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) ); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 48. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange $cache = new LiveNinjaStorageCache(); // Act $cache->add(‘user_ids’, [ 1, 2, 3 ]); // Assert $this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) ); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 49. <?php class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase { // test cases ... public function testSetWithNewDataPersistsToCache() { // Arrange $cache = new LiveNinjaStorageCache(); // Act $cache->add(‘user_ids’, [ 1, 2, 3 ]); // Assert $this->assertSame( [ 1, 2, 3 ], $cache->get(‘user_ids’) ); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Time: 125 ms, Memory: 2.35Mb OK (1 test, 1 assertions) Saturday, October 5, 13
  • 50. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu object|WP_Error UserService::persist(User $user); Saturday, October 5, 13
  • 51. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 52. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange // Act // Assert $this->assertInstanceOf(‘WP_Error’, $user); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 53. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange // Act $user = $service->persist($inCompleteUserObject); // Assert $this->assertInstanceOf(‘WP_Error’, $user); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 54. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange $service = new LiveNinjaUserService; $inCompleteUserObject = new LiveNinjaUserEntity(...); // Act $user = $service->persist($inCompleteUserObject); // Assert $this->assertInstanceOf(‘WP_Error’, $user); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 55. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testPersistReturnsWPErrorIfItDoesntContainRequiredFields() { // Arrange $service = new LiveNinjaUserService; $inCompleteUserObject = new LiveNinjaUserEntity(...); // Act $user = $service->persist($inCompleteUserObject); // Assert $this->assertInstanceOf(‘WP_Error’, $user); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Time: 85 ms, Memory: 1.25Mb OK (1 test, 1 assertions) Saturday, October 5, 13
  • 56. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu array|WP_Error UserService::get_ninjas(array $args); Saturday, October 5, 13
  • 57. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange // Act // Assert } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 58. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange // Act // Assert $this->assertContains($expected, $service->getLastResultAsSQL()); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 59. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange // Act $service->get_ninjas([‘plan’ => ‘blackbelt’]); // Assert $this->assertContains($expected, $service->getLastResultAsSQL()); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 60. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange $expected = “SELECT * FROM ninjas WHERE ...”; $service = new LiveNinjaUserService; // Act $service->get_ninjas([‘plan’ => ‘blackbelt’]); // Assert $this->assertSame($expected, $service->getLastResultAsSQL()); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  • 61. <?php class UserServiceTest extends PHPUnit_Framework_TestCase { // test cases ... public function testGetNinjasWithBlackBeltSubscriptionQuery() { // Arrange $expected = “SELECT * FROM ninjas WHERE ...”; $service = new LiveNinjaUserService; // Act $service->get_ninjas([‘plan’ => ‘blackbelt’]); // Assert $this->assertContains($expected, $service->getLastResultAsSQL()); } } UNIT TESTING like A PIRATE — @ptahdunbar #wceu Time: 101 ms, Memory: 1.55Mb OK (1 test, 1 assertions) Saturday, October 5, 13
  • 62. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nWAYS TO TEST CODE • test if/else conditionals • test all cases in switch • verify loops contain proper data. • Check for expected state • Check return value type • Pass unexpected data • Verify correct amount • assertContains for SQL Queries • Verify that third party method was called. • $this->assertType(); • etc... Saturday, October 5, 13
  • 63. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nWAYS TO kickstart your brain • write out list of requirements for each feature • do the UI first Saturday, October 5, 13
  • 65. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Thank you! Slides - http://ptahdunbar.com/ cPirate Dunbar Saturday, October 5, 13