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

Concurrent Test Frameworks

Concurrent Test Frameworks

Testing applications is one of the most important thing in a developer’s toolbox.
Sometime writing tests is not so straightforward and as time goes by you notice that you have to maintain your application and your tests too.
The purpose of this talk is to show how to test your domain at different layers with different test frameworks, in order to ease the development process and transform your codebase in a stronghold.

21935bc100d813458e925f4a0bef80db?s=128

Andrea Giuliano

May 15, 2015
Tweet

More Decks by Andrea Giuliano

Other Decks in Technology

Transcript

  1. CONCURRENT TEST FRAMEWORKS Andrea Giuliano @bit_shark

  2. Andrea Giuliano @bit_shark andreagiuliano.it $WHOAMI

  3. PHP TEST FRAMEWORKS

  4. WHICH ONE SHOULD I USE? CONCENTRATE AND ASK AGAIN

  5. WHO SAID “ONE”?

  6. IS THE TOOL THE PROBLEM?

  7. LET’S START FROM THE BEGINNING

  8. TEST DRIVEN DEVELOPMENT Red Green Refactor

  9. EMERGENT DESIGN

  10. TDD AS A PROBLEM

  11. TDD AS A PROBLEM class VideoGalleryTest extends \PHPUnit_Framework_TestCase { public

    function testUploadVideo() { $videoGallery = new VideoGallery(); $videoGallery->upload('path/on/my/awesome/file.mp4'); $this->assertTrue(is_array($videoGallery->getVideos())); $this->assertCount(1, $videoGallery->getVideos()); $this->assertInstanceOf( Video::class, $videoGallery->getFirstVideo()); } }
  12. TDD AS A PROBLEM class VideoGalleryTest extends \PHPUnit_Framework_TestCase { public

    function testUploadVideo() { $videoGallery = new VideoGallery(); $videoGallery->upload('path/on/my/awesome/file.mp4'); $this->assertTrue(is_array($videoGallery->getVideos())); $this->assertCount(1, $videoGallery->getVideos()); $this->assertInstanceOf( Video::class, $videoGallery->getFirstVideo()); } } testing internals
  13. IS VERSUS DOES

  14. IT’S ALL ABOUT BEHAVIOUR

  15. DESCRIBING A STORY Feature: As a [role] I want [feature]

    So that [benefit] Scenario: scenario description Given [context] When [event] Then [outcome]
  16. A DIFFERENT CYCLE Write an acceptance test (Given/When/Then) Discover ‘verbs’,

    ‘nouns’ and write a failing step definition
  17. A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’

    Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition
  18. A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’

    Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  19. A DIFFERENT CYCLE Describe the behaviours for ‘verbs’ and ‘nouns’

    Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  20. THE TOOL composer require behat/behat --dev

  21. DESCRIBING A STORY Scenario: Getting the total amount for a

    conference ticket Given a ticket for the "PHP Day" conference priced EUR100 was added to the marketplace When a user adds the "PHP Day" ticket from the marketplace to the user's shopping cart Then the overall cart price should be EUR122
  22. RUN THE STORY $ bin/behat --append-snippets

  23. VERBS, NOUNS, BEHAVIOUR DISCOVERING /** * @Given a ticket for

    the :conference conference priced EUR:price * was added to the marketplace */ public function aTicketForTheConferencePriced($conference, $price) { $money = Money::EUR((int) $price); $ticket = Ticket::forConferencePriced($conference, $money); $this->marketPlace->add($ticket); }
  24. /** * @When a user adds the ":conference" ticket *

    from the marketplace to the shopping cart */ public function aUserAddTheTicketToTheShoppingCart($conference) { $this->user->addTicketToCart($conference, $this->marketPlace); } VERBS, NOUNS, BEHAVIOUR DISCOVERING
  25. /** * @Then the overall cart price should be EUR:price

    */ public function theOverallCartPriceShouldBe($price) { $money = Money::EUR((int) $price); $amount = $this->user->shoppingCart()->total(); if (!$amount->equals($money)) { throw new \Exception("Cart amount is not " . $price); } } VERBS, NOUNS, BEHAVIOUR DISCOVERING
  26. DESCRIBE THE BEHAVIOUR Describe the behaviours for ‘verbs’ and ‘nouns’

    Write an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  27. THE TOOLS

  28. DESCRIBING TICKET CREATION class TicketTest extends \PHPUnit_Framework_TestCase { /** *

    @test */ function should_create_a_ticket_for_conference_priced() { $price = Money::EUR(1000); $ticket = Ticket::forConferencePriced( 'An awesome conference', $price ); $this->assertInstanceOf(Ticket::class, $ticket); } }
  29. OBJECT CREATION Describe the behaviours for ‘verbs’ and ‘nouns’ Write

    an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  30. THE TICKET OBJECT class Ticket implements TicketInterface { private $event;

    private $price; private function __construct($event, Money $price) { $this->event = $event; $this->price = $price; } public static function forConferencePriced($conference, Money $price) { return new Ticket($conference, $price); } }
  31. GOING UP Describe the behaviours for ‘verbs’ and ‘nouns’ Write

    an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  32. SHOPPING CART SPECIFICATION class ShoppingCartSpec extends ObjectBehavior { function it_should_return_the_amount_with_vat(TicketInterface

    $ticket) { $ticket->price()->willReturn(Money::EUR(100.00)); $this->addTicket($ticket); $expectedMoney = Money::EUR(122.00); $this->total()->shouldBeLike($expectedMoney); } }
  33. THE SHOPPING CART OBJECT class ShoppingCart { public function total()

    { $total = Money::EUR(0); foreach ($this->tickets as $ticket) { $ticketPriceWithVat = Money::multiply( $ticket->price(), self::VAT ); $total = Money::add($total, $ticketPriceWithVat->amount()); } return $total; } }
  34. GOING UP Describe the behaviours for ‘verbs’ and ‘nouns’ Write

    an acceptance test (Given/When/Then) Discover ‘verbs’, ‘nouns’ and write a failing step definition Create object and the behavioural methods
  35. THE GREEN SATISFACTION

  36. MOVING OUTWARDLY Domain Application Presentation

  37. THE FORTRESS

  38. joind.in/14553 Please rate the talk

  39. joind.in/14553 Please rate the talk

  40. REFERENCES Assets: https://www.flickr.com/photos/levork/4966756896 https://www.flickr.com/photos/jenny-pics/7960912752 https://www.flickr.com/photos/joel_baker/12484614505 https://www.flickr.com/photos/mike-f/8628898387 https://www.flickr.com/photos/mike-f/8628898387 https://www.flickr.com/photos/michaeljzealot/6485009571 The RSpec

    book, Behaviour Driven Development with RSpec, Cucumber, and Friends - D. Chelimsky Konstantin Kudryashov aka Everzet - Introducing modeling by example everzet.com/post/99045129766/introducing-modelling-by-example a special thanks to Giulio De Donato aka liuggio