Your SlideShare is downloading. ×
0
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Unit Test Fun
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Unit Test Fun

7,338

Published on

Published in: Technology
1 Comment
1 Like
Statistics
Notes
  • i have bookmark it thanks
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
7,338
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
27
Comments
1
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Unit Test Fun: Mock Objects, Fixtures,Stubs & Dependency InjectionMax Köhler I 02. Dezember 2010 © 2010 Mayflower GmbH
  • 2. Wer macht UnitTests? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 2
  • 3. Code Coverage? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 3
  • 4. Wer glaubt, dass die Tests gut sind? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 4
  • 5. Kann die Qualitätgesteigert werden? 100% 0% Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 5
  • 6. Test der kompletten Architektur? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 6
  • 7. MVC? ControllerView Model Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 7
  • 8. Wie testet Ihr eure Models? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 8
  • 9. Direkter DB-Zugriff? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 9
  • 10. Keine UnitTests! STOP Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 10
  • 11. Integration Tests! Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 11
  • 12. Wie testet Ihr eure Controller? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 12
  • 13. Routes, Auth-Mock, Session-Mock, ...? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 13
  • 14. Keine UnitTests! STOP Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 14
  • 15. Was wollen wir testen? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 15
  • 16. Integration Regression Testing Testing Unit TestingSystem - Integration Testing Acceptance Testing System Testing Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 16
  • 17. The goal of unit testing isto isolate each part of the program and show that the individual parts are correct Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 17
  • 18. Test Doubles Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 18
  • 19. StubsUnit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 19
  • 20. Fake that returns canned data... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 20
  • 21. Beispiel für ein Auth-Stub $storageData = array( accountId => 29, username => Hugo, jid => hugo@example.org); $storage = $this->getMock(Zend_Auth_Storage_Session, array(read)); $storage->expects($this->any()) ->method(read) ->will($this->returnValue($storageData)); Zend_Auth::getInstance()->setStorage($storage); // ... /* * Bei jedem Aufruf wird nun das Mock als Storage * verwendet und dessen Daten ausgelesen */ $session = Zend_Auth::getInstance()->getIdentity(); Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 21
  • 22. MocksUnit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 22
  • 23. Spy withexpectations... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 23
  • 24. Model Mapper Beispiel class Application_Model_GuestbookMapper { protected $_dbTable; public function setDbTable(Zend_Db_Table_Abstract $dbTable) { $this->_dbTable = $dbTable; return $this; } public function getDbTable() { return $this->_dbTable; } public function getEmail() {} public function getComment() {} public function save(Application_Model_Guestbook $guestbook) { $data = array( email => $guestbook->getEmail(), comment => $guestbook->getComment(), ); $this->getDbTable()->insert($data); } } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 24
  • 25. Testen der save() Funktionclass Applicatoin_Model_GuestbookMapperTest extends PHPUnit_Framework_TestCase{ public function testSave() { $modelStub = $this->getMock(Application_Model_Guestbook, array(getEmail, ,getComment)); $modelStub->expects($this->once()) ->method(getEmail) ->will($this->returnValue(super@email.de)); $modelStub->expects($this->once()) ->method(getComment) ->will($this->returnValue(super comment)); $tableMock = $this->getMock(Zend_Db_Table_Abstract, array(insert), array(), , false); $tableMock->expects($this->once()) ->method(insert) ->with($this->equalTo(array( email => super@email.de, comment => super comment))); $model = new Application_Model_GuestbookMapper(); $model->setDbTable($tableMock); // << MOCK $model->save($modelStub); // << STUB }} Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 25
  • 26. Stub Mock Fake that Spy withreturns canned !== expectations... data... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 26
  • 27. FixturesUnit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 27
  • 28. Set the world up in a known state ... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 28
  • 29. Fixture-Beispiel class Fixture extends PHPUnit_Framework_TestCase { protected $fixture; protected function setUp() { $this->fixture = array(); } public function testEmpty() { $this->assertTrue(empty($this->fixture)); } public function testPush() { array_push($this->fixture, foo); $this->assertEquals(foo, $this->fixture[0]); } public function testPop() { array_push($this->fixture, foo); $this->assertEquals(foo, array_pop($this->fixture)); $this->assertTrue(empty($this->fixture)); } } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 29
  • 30. Method Stack Ablauf public static function setUpBeforeClass() { } protected function setUp() { } public function testMyTest() { /* TEST */ } protected function tearDown() { } protected function onNotSuccessfulTest(Exception $e) { } public static function tearDownAfterClass() { } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 30
  • 31. Test Suite ... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 31
  • 32. ...wirkt sich auf die Architektur aus. Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 32
  • 33. Wenn nicht... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 33
  • 34. DeveloperTitel der Präsentation I Mayflower GmbH I xx. Juni 2010 I 34
  • 35. Was kann man machen? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 35
  • 36. Production Code überarbeiten Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 36
  • 37. Dependency Injection Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 37
  • 38. Bemerkt?Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 38
  • 39. Dependency Injection class Application_Model_GuestbookMapper { protected $_dbTable; public function setDbTable(Zend_Db_Table_Abstract $dbTable) { $this->_dbTable = $dbTable; return $this; } public function getDbTable() { return $this->_dbTable; } public function getEmail() {} public function getComment() {} public function save(Application_Model_Guestbook $guestbook) { $data = array( email => $guestbook->getEmail(), comment => $guestbook->getComment(), ); $this->getDbTable()->insert($data); } } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 39
  • 40. Besser aber ... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 40
  • 41. ... Begeisterung sieht anders aus! Titel der Präsentation I Mayflower GmbH I xx. Juni 2010 I 41
  • 42. Was könnte noch helfen? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 42
  • 43. TDD?Test Driven Development [ ~~ [ ~~ ~~ ~~ ~~ ~~ ~~ ~~ ~ Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 43
  • 44. Probleme früh erkennen! Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 44
  • 45. Uncle Bob´sThree Rules of TDD Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 45
  • 46. #1 “ You are not allowed to write anyproduction code unless it is to make a failing unit test pass. Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 46
  • 47. #2“ You are not allowed to write any more ofa unit test than is sufficient to fail; and compilation failures are failures. Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 47
  • 48. #3“ You are not allowed to write any moreproduction code than is sufficient to pass the one failing unit test. Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 48
  • 49. Und wieder... Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 49
  • 50. DeveloperTitel der Präsentation I Mayflower GmbH I xx. Juni 2010 I 50
  • 51. Und jetzt?Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 51
  • 52. Things get worst before they get better ! Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 52
  • 53. Monate später... Titel der Präsentation I Mayflower GmbH I xx. Juni 2010 I 53
  • 54. Gibts noch was? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 54
  • 55. Darf ich vorstellen:„Bug“ Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 55
  • 56. Regression Testing or Test your Bugs! Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 56
  • 57. Regression Testing Bug class Calculate { public function divide($dividend, $divisor) 1 { return $dividend / $divisor; } } 2 Warning: Division by zero in /srv/phpunit-slides/Calculate.php on line 7 Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 57
  • 58. Regression Testing Test First! /** * Regression-Test BUG-123 * * @group BUG-123 * * @return void 3 */ public function testDivideByZero() { $calc = new Calculate(); $this->assertEquals(0, $calc->divide(1, 0)); } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 58
  • 59. Regression Testing Bugfix class Calculate { 4 public function divide($dividend, $divisor) { if (0 == $divisor) { return 0; } return $dividend / $divisor; } } Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 59
  • 60. Regression Testing phpunit slides$ phpunit --colors --verbose CalculateTest.php PHPUnit 3.5.5 by Sebastian Bergmann. 5 CalculateTest ...... Time: 0 seconds, Memory: 5.25Mb OK (6 tests, 6 assertions) Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 60
  • 61. Regression Testing @group slides$ phpunit --colors --verbose --group BUG-123 CalculateTest.php PHPUnit 3.5.5 by Sebastian Bergmann. ? CalculateTest . Time: 0 seconds, Memory: 5.25Mb OK (1 tests, 1 assertions) /** * Regression-Test BUG-123 * * @group BUG-123 * * @return void ? */ public function testDivideByZero() { Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 61
  • 62. Noch Fragen? Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 62
  • 63. QuellenI Baby: ADDROX http://www.flickr.com/photos/addrox/2587484034/sizes/m/I Fish: ADDROX http://www.flickr.com/photos/addrox/274632284/sizes/m/I Happy: ADDROX http://www.flickr.com/photos/addrox/2610064689/sizes/m/I Bug: ADDROX http://www.flickr.com/photos/addrox/284649644/sizes/m/ Unit Test Fun: Mock Objects, Fixtures, Stubs & Dependency Injection I Mayflower GmbH I 02. Dezember 2010 I 63
  • 64. Vielen Dank für Ihre Aufmerksamkeit!Kontakt Max Köhler max.koehler@mayflower.de +49 89 242054-1160 Mayflower GmbH Mannhardtstr. 6 80538 München © 2010 Mayflower GmbH

×