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...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nAPP Platform
WordPress as a
“I think WordPress is going in a
direction tha...
“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
...
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 ☹
Sat...
• sdsdsd
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Problems of manual testing
1. Ad-hoc ☹
2. Slow & Error Prone ☹
3. ...
• 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, O...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what we need
• Fast & Instant feedback
• Reusable and organized
• Verify th...
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 ...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is unit testing
“An automated piece of code that
invokes your applicat...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is unit testing
“Separating
the application
design &
implementation
pr...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
“A command line
tool that runs
unit tests &
reports their
r...
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)
...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
There was 1 failure:
1) WPSkeletonPluginTestAutoloadTest::t...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
Test Case
a set of conditions or
variables that you set
up ...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
what is PHPUNIT
Test SUITE
A collection of test cases.
Saturday, October 5,...
<?php
class SubscriptionTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testHasAccessWithPassD...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Assertions
“a way of explicitly checking the
assumptions that your code
mak...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
n• $this->assertTrue();
• $this->assertEquals();
• $this->assertContains();...
“How do I actually write
out the test cases?”
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
http://www.flickr.com/photos/m...
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)
Sa...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. A
2. Act(call the method/trigger the action)
3. A...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
Anatomy of a test case
1. arrange (the context)
2. Act(call the method/trig...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class CalculatorTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testAddReturnsSumOfTwoPo...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class LiveNinjaCacheTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testSetWithNewDataPe...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testPersistReturnsWPErr...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
<?php
class UserServiceTest extends PHPUnit_Framework_TestCase
{
// test cases ...
public function testGetNinjasWithBlackB...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nWAYS TO TEST CODE
• test if/else conditionals
• test all cases in switch
•...
UNIT TESTING like A PIRATE — @ptahdunbar #wceu
nWAYS TO kickstart your brain
• write out list of requirements for
each fea...
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...
Upcoming SlideShare
Loading in …5
×

Unit testing like a pirate #wceu 2013

11,005 views

Published on

Published in: Technology, Education
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
11,005
On SlideShare
0
From Embeds
0
Number of Embeds
7,588
Actions
Shares
0
Downloads
30
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Unit testing like a pirate #wceu 2013

  1. 1. UNIT TESTING likea cPirate WordCamp Europe 2013 with pirate dunbar Saturday, October 5, 13
  2. 2. UNIT TESTING like A PIRATE — @ptahdunbar #wceu YES, I am a Pirate. Saturday, October 5, 13
  3. 3. UNIT TESTING like A PIRATE — @ptahdunbar #wceu nAPP Platform WordPress as a Saturday, October 5, 13
  4. 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. 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. 6. “Plugins represent the heartbeat of WordPress.” UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  7. 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. 8. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Manual Testing Saturday, October 5, 13
  9. 9. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Problems of manual testing 1. Ad-hoc ☹ Saturday, October 5, 13
  10. 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. 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. 12. • sdsdsd UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  13. 13. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need Saturday, October 5, 13
  14. 14. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need • Fast & Instant feedback Saturday, October 5, 13
  15. 15. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what we need • Fast & Instant feedback • Reusable and organized Saturday, October 5, 13
  16. 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. 17. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  18. 18. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  19. 19. Unit Testing Series UNIT TESTING like A PIRATE — @ptahdunbar #wceu Saturday, October 5, 13
  20. 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. 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. 22. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is unit testing “Separating the application design & implementation process.” Saturday, October 5, 13
  23. 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. 24. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT $>phpunit Saturday, October 5, 13
  25. 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. 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. 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. 28. UNIT TESTING like A PIRATE — @ptahdunbar #wceu what is PHPUNIT Test SUITE A collection of test cases. Saturday, October 5, 13
  29. 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. 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. 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. 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. 33. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case Saturday, October 5, 13
  34. 34. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Anatomy of a test case 1. a 2. A 3. A Saturday, October 5, 13
  35. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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
  64. 64. cwp skeleton plugin https://github.com/ptahdunbar/wp-skeleton-plugin/ Saturday, October 5, 13
  65. 65. UNIT TESTING like A PIRATE — @ptahdunbar #wceu Thank you! Slides - http://ptahdunbar.com/ cPirate Dunbar Saturday, October 5, 13

×