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

Unit Testing With SilverStripe

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Unit Testing With SilverStripe

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

wernerkrauss

October 14, 2017

Transcript

47 slides
  1. 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
  2. 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
  3. 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
  4. 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
  5. Quotes • Testing your code requires you to change how

    you write code (Chris Hartjes) 13.10.2017 www.silverstrip.es 7
  6. 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
  7. 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
  8. 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
  9. 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
  10. How Does A Test Work? • Arrange • Act •

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

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

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

    • Throw a meaningful message on failure 13.10.2017 www.silverstrip.es 20
  14. 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
  15. 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
  16. Red – Green - Refactor Write a failing test Make

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

    • Make it fast 13.10.2017 www.silverstrip.es 25
  18. 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
  19. What Should I Test? • Expected results • Return types

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

    start • Legacy code is no excuse 13.10.2017 www.silverstrip.es 29
  21. 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
  22. PHPUnit • Written by Sebastian Bergmann • Since 2004 •

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

    EmailTest • A test method starts with „test“, e.g. testEmailSentWhenNewUserRegisters() 13.10.2017 www.silverstrip.es 33
  24. 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
  25. SilverStripe Specific Assertions • assertEmailSentTo • assertSQLContains … • assertDOSContains

    (assertListContains) • assertDOSEquals (assertListEquals) • assertDOSAllMatch (assertListAllMatch) 13.10.2017 www.silverstrip.es 35 SS4
  26. 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
  27. Test As Documentation • --testdox – Automatic generated documentation from

    test names (or annotation) 13.10.2017 www.silverstrip.es 42
  28. Debug a Unit Test • Combine the strength of two

    power tools • Needs CLI-Debugging configured 13.10.2017 www.silverstrip.es 44
  29. 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

More Decks by wernerkrauss

Other Decks in Technology