$30 off During Our Annual Pro Sale. View Details »

Test-driven development with PHPUnit

Test-driven development with PHPUnit

Oliver Klee

August 04, 2023
Tweet

More Decks by Oliver Klee

Other Decks in Programming

Transcript

  1. Test-driven

    Development

    with PHPUnit
    Oliver Klee, 2023-08
    @[email protected] | [email protected]

    View Slide

  2. About me
    Oliver „Oli“ Kle
    e

    #bon
    n

    #extension-de
    v

    #workshop-teache
    r

    #unit-testing-gu
    y

    #phpstan-gu
    y

    #best-practices-tea
    m

    #motivation-research-tea
    m

    #team-leadership-working-grou
    p

    #game-cookin
    g

    #powermetal

    View Slide

  3. Unit

    Tests?

    View Slide

  4. Manual testing is cumbersome

    View Slide

  5. Automated

    testing

    is fast

    View Slide

  6. Unit tests for the

    TYPO3 Core

    View Slide

  7. Unit Tests

    406 (144) tests/s

    View Slide

  8. Functional Tests

    8.13 (1.97) tests/s

    View Slide

  9. Acceptance Tests

    .009 (.003) tests/s

    View Slide

  10. Verify that your code
    does what you expect

    View Slide

  11. Make sure that

    your changes won‘t

    break anything

    View Slide

  12. Keep others

    from breaking your code

    View Slide

  13. Don’t break anything

    even in complex

    projects

    View Slide

  14. Create a safety net

    for refactoring
    or for TYPO3, PHP
    or Symfony updates

    View Slide

  15. Improve
    the structure
    of your code

    View Slide

  16. Find the location of bugs

    View Slide

  17. Green feels good!

    View Slide

  18. Let’s get

    some terms

    straight

    View Slide

  19. The two programming modes
    clean
    hacky

    View Slide

  20. Testsuite
    Testcase
    Test
    Assertion
    Test
    Test
    Testcase

    View Slide

  21. The life cycle of a test
    new FooTest();
    setUp();
    /** @test */


    lifeIsGood();
    tearDown();

    View Slide

  22. 4 test phases
    set up
    setUp()

    code in the test method
    exercise method call
    verify assert…()
    tear down tearDown()

    View Slide

  23. Code test-first
    write

    test
    write

    code
    refactor

    View Slide

  24. Effects of TDD
    higher code
    coverage
    minimale

    code
    think before
    you code
    tests now test
    what the code
    should do
    (instead of
    what it does)
    focused coding
    cleaner code
    structure

    View Slide

  25. Test levels
    http://
    fi
    lipdefar.com/2015/06/tested-be-thy-name.html

    View Slide

  26. Unit tests are

    small and fast

    View Slide

  27. Integration tests
    test how components

    work together
    in TYPO3:

    „functional tests“

    View Slide

  28. Acceptance tests
    Codeception Cypress

    View Slide

  29. Test types

    View Slide

  30. Blackbox tests
    test


    the public API

    View Slide

  31. Whitebox tests
    inner workings
    test the

    View Slide

  32. Anti-

    zombie
    tests-

    View Slide

  33. Regression tests

    View Slide

  34. Smoke tests

    View Slide

  35. Smoke tests

    View Slide

  36. Story tests for


    Behavior-driven developmen
    t

    (BDD)
    Behat
    Codeception

    View Slide

  37. The testing pyramid
    Unit
    Functional/

    Integration
    Acceptance
    Photo by Eugene Tkachenko on Unsplash: https://unsplash.com/photos/TF47p5PHW18

    View Slide

  38. Naming

    Tests

    View Slide

  39. Use meaningful test names

    classCanBeInstantiated
    setTitleSetsTitle
    setSizeWithZeroThrowsException


    hasTitleForEmptyTitleReturnsFalse
    Name the
    behavior.
    Name the
    preconditions.
    Name the
    method.
    Don’t use

    “works” or

    “works correctly”.
    measureFrubbleWorksCorrectly

    View Slide