Unit Testing With SilverStripe

Unit Testing With SilverStripe

PHPUnit talk hold at the SilverStripe Conference 2017 in Malta. See www.stripecon.eu

9748d71c4e18135a4f044d5b93af0ee3?s=128

wernerkrauss

October 14, 2017
Tweet

Transcript

  1. None
  2. What Is Testing? • A piece of code that tests

    your code • A scenario how your code should work 13.10.2017 www.silverstrip.es 2
  3. Why Should I Test? • It‘s fun! 13.10.2017 www.silverstrip.es 3

  4. Why Should I Test? • Don‘t we already test our

    code? • F5-Driven Development – Feedback Loop (var dump etc…) • Pray on deployment 13.10.2017 www.silverstrip.es 4
  5. Why Should I Test Automatically • A computer is good

    in processing things over and over again • Without complaining 13.10.2017 www.silverstrip.es 5
  6. Quotes • Tests for a project mean that at least

    somebody used to care at some point about what they were doing (Chris Hartjes) 13.10.2017 www.silverstrip.es 6
  7. Quotes • Testing your code requires you to change how

    you write code (Chris Hartjes) 13.10.2017 www.silverstrip.es 7
  8. Quotes 13.10.2017 www.silverstrip.es 8

  9. Quotes 13.10.2017 www.silverstrip.es 9

  10. Quotes 13.10.2017 www.silverstrip.es 10

  11. BUT… 13.10.2017 www.silverstrip.es 11

  12. Contra Testing • Writing tests takes more time • You

    have to reason about what you‘re doing • It‘s not easy to write good and meaningful tests • No 100% guarantee that your code is bug free 13.10.2017 www.silverstrip.es 12
  13. Contra Testing #2 • Running them takes a long time

    (DB connection is slow) • I‘m the only one in the company that runs my tests • You have to change your habbits 13.10.2017 www.silverstrip.es 13
  14. Pro Testing • It‘s just code – and we know

    how to code • Repeatable – don‘t throw away your (manual) tests • Future investment • 90% less bugs in production • Bugs are found during development phase, not after shipping – That‘s cheaper 13.10.2017 www.silverstrip.es 14
  15. Pro Testing #2 • Makes you reason about your code

    – Structure – How the code should solve the given problem • Documentation of how your code is supposed to work • Makes you a better developer • Refactoring becomes easier 13.10.2017 www.silverstrip.es 15
  16. ANATOMY OF A TEST 13.10.2017 www.silverstrip.es 16

  17. How Does A Test Work? • Arrange • Act •

    Assert 13.10.2017 www.silverstrip.es 17
  18. Arrange • Get your prerequisites • Identify dependencies • Fixtures,

    Mocks… • Define the expected result 13.10.2017 www.silverstrip.es 18
  19. Act • Call the method you test • and grab

    the result 13.10.2017 www.silverstrip.es 19
  20. Assert • Compare your result or behaviour with the expectation

    • Throw a meaningful message on failure 13.10.2017 www.silverstrip.es 20
  21. Types Of Tests • Unit tests – Small units of

    your code, usually a method • Component tests – Bigger unit, – might use database – takes longer to run • Functional test – End to end, incl. GET / POST request etc.. 13.10.2017 www.silverstrip.es 21
  22. WHAT IS „TEST DRIVEN DEVELOPMENT“? 13.10.2017 www.silverstrip.es 22

  23. Test Driven Development (TDD) • Write your test first •

    Run that test – Watch it failing • Write code to fulfill your test • Run the test again – Watch it passing • Refactor your code 13.10.2017 www.silverstrip.es 23
  24. Red – Green - Refactor Write a failing test Make

    that test pass Clean up your code 13.10.2017 www.silverstrip.es 24
  25. TDD Workflow • Make it work • Make it right

    • Make it fast 13.10.2017 www.silverstrip.es 25
  26. TDD Pro • Fast Feedback loop with small iterations •

    Design your code in a testable manner • More modular • Cleaner interfaces • „Stupid“ mistakes are found immediately 13.10.2017 www.silverstrip.es 26
  27. LET‘S PLAY! 13.10.2017 www.silverstrip.es 27

  28. What Should I Test? • Expected results • Return types

    • Error handling • Thrown Exceptions • Etc… 13.10.2017 www.silverstrip.es 28
  29. Start Writing Tests • A new module is a good

    start • Legacy code is no excuse 13.10.2017 www.silverstrip.es 29
  30. Testing A Legacy Project • Code might be harder to

    test • You have to reason about the existing code • Tests create a backlog for technical debt – Technial debt is a bad thing that backfires. Always • Proof that legacy code works – or not • A safeguard for refactoring 13.10.2017 www.silverstrip.es 30
  31. PHPUNIT 13.10.2017 www.silverstrip.es 31

  32. PHPUnit • Written by Sebastian Bergmann • Since 2004 •

    Instance of xUnit architecture 13.10.2017 www.silverstrip.es 32
  33. PHPUnit Conventions • Naming: – Class Email – Test class:

    EmailTest • A test method starts with „test“, e.g. testEmailSentWhenNewUserRegisters() 13.10.2017 www.silverstrip.es 33
  34. PHPUnit & SilverStripe • Tests subclass SapphireTest • Provides yml

    Fixtures • Custom assertions for SilverStripe • Switch to test without fixtures / db setup (SilverStripe 3.5+) 13.10.2017 www.silverstrip.es 34
  35. SilverStripe Specific Assertions • assertEmailSentTo • assertSQLContains … • assertDOSContains

    (assertListContains) • assertDOSEquals (assertListEquals) • assertDOSAllMatch (assertListAllMatch) 13.10.2017 www.silverstrip.es 35 SS4
  36. PHPUNIT GOODIES 13.10.2017 www.silverstrip.es 36

  37. Data Providers • Write more generic tests, feed different data

    to it • You can reuse them, also call from different classes 13.10.2017 www.silverstrip.es 37
  38. Before 13.10.2017 www.silverstrip.es 38

  39. After 13.10.2017 www.silverstrip.es 39

  40. 13.10.2017 www.silverstrip.es 40

  41. Order Of Tests • @depends • TODO: Code example 13.10.2017

    www.silverstrip.es 41
  42. Test As Documentation • --testdox – Automatic generated documentation from

    test names (or annotation) 13.10.2017 www.silverstrip.es 42
  43. testdox 13.10.2017 www.silverstrip.es 43

  44. Debug a Unit Test • Combine the strength of two

    power tools • Needs CLI-Debugging configured 13.10.2017 www.silverstrip.es 44
  45. Further Resources https://leahttps://leanpub.com/u/chartjesb.c om/u/chartjes 13.10.2017 www.silverstrip.es 45 https://leanpub.com/u/chartjes

  46. About me • Werner M. Krauß • Located in Hallstatt,

    Austria • wmk on IRC slack / github / stackoverflow • PHP since 1998 • Freelancer since 2006 (netwerkstatt) • SilverStripe since 2009
  47. Thank you! www.silverstrip.es