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

Test-driven development with PHPUnit

Test-driven development with PHPUnit

Oliver Klee

August 05, 2022
Tweet

More Decks by Oliver Klee

Other Decks in Programming

Transcript

  1. Test-driven

    development

    with PHPUnit
    Oliver Klee, 2022-08
    @oliklee | Mastodon: [email protected] | [email protected]

    View Slide

  2. About me
    Oliver „Oli“ Klee


    #bonn


    #extension-dev


    #workshop-teacher


    #unit-testing-guy


    #phpstan-guy


    #typo3-ombudsperson


    #typo3-quality-assurance-initiative


    #gami
    fi
    cation-working-group


    #game-cooking


    #powermetal

    View Slide

  3. ???

    View Slide

  4. The goals
    ✓ You have a clearer
    understanding on
    how TDD works.
    ✓(optional) You get
    test-infected.
    ✓You learn at least 1
    new testing pattern.
    ✓We all have fun.

    View Slide

  5. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide

  6. The rules


    1. There are no
    stupid questions.


    2. The joker rule.

    View Slide

  7. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide

  8. Unit

    Tests?

    View Slide

  9. Manual testing is cumbersome

    View Slide

  10. Automated

    testing

    is fast

    View Slide

  11. Unit tests for the

    TYPO3 Core

    View Slide

  12. Unit Tests

    406 (144) tests/s

    View Slide

  13. Functional Tests

    8.13 (1.97) tests/s

    View Slide

  14. Acceptance Tests

    .009 (.003) tests/s

    View Slide

  15. Verify that your code
    does what you expect

    View Slide

  16. Make sure that

    your changes won‘t

    break anything

    View Slide

  17. Keep others

    from breaking your code

    View Slide

  18. Don’t break anything

    even in complex

    projects

    View Slide

  19. Create a safety net

    for refactoring
    or for

    TYPO3 updates

    View Slide

  20. Improve
    the structure
    of your code

    View Slide

  21. Find the location of bugs

    View Slide

  22. Green feels good!

    View Slide

  23. Let’s get

    some terms

    straight

    View Slide

  24. The two programming modes
    clean
    hacky

    View Slide

  25. Testsuite
    Testcase
    Test
    Assertion
    Test
    Test
    Testcase

    View Slide

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


    lifeIsGood();
    tearDown();

    View Slide

  27. Code test-first
    write

    test
    write

    code
    refactor

    View Slide

  28. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide

  29. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide

  30. https://github.com/TYPO3-Documentation/tea/


    Branch: workshop/t3dd22


    File: Exercises.md

    View Slide

  31. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide

  32. Naming

    Tests

    View Slide

  33. 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

  34. The travel plan
    1. getting starte
    d

    2. concepts & term
    s

    3. testing pattern
    s

    4. hands-o
    n

    5. naming test
    s

    6. hands-o
    n

    7. ending & feedback

    View Slide