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

Advanced TDD

Diego Aguiar
December 02, 2022

Advanced TDD

If you love TDD and want to improve in this programming discipline, you're in the right place. Attend this talk, and you'll learn about:

- What TDD really is
- Techniques for making your test suite more reliable
- What does getting stuck mean, and how to get out of it
- Why mocking is more about collaboration
- Acceptance tests and more!

Diego Aguiar

December 02, 2022
Tweet

Other Decks in Programming

Transcript

  1. ADVANCED TDD
    Diego Aguiar Beltrán
    @MolloKhan

    View Slide

  2. ➢ Developer at SymfonyCasts
    ➢ Hiking
    ➢ Gaming (Warcraft FTW!)
    ➢ Tepic, Mexico
    Diego Aguiar Beltrán
    @MolloKhan

    View Slide

  3. Main Topics
    ➢ What TDD Really is
    ➢ Useful TDD Techniques
    ➢ What “Getting Stuck” means
    ➢ When Not to TDD

    View Slide

  4. What is TDD?
    ➢ Programming Methodology
    ➢ It’s Composed of 3 Laws
    ➢ Tests Always Go First
    ➢ Red, Green, Refactor cycle

    View Slide

  5. TDD is more like a
    discipline

    View Slide

  6. ➢ Practice, practice, practice…
    ➢ Train Your Mind
    ➢ Tests Go First

    View Slide

  7. A bit of History…
    ➢ Kent Beck inventor of TDD
    ➢ Invented In the late 1990’s
    ➢ As a way to write better quality code

    View Slide

  8. TDD
    Techniques
    ➢ Fake it till you make it
    ➢ Triangulation
    ➢ Assert First

    View Slide

  9. Fake it till you make it

    View Slide

  10. Name Inverter
    Example
    “Diego Aguiar” → “Aguiar, Diego”
    Fake it till you make it

    View Slide

  11. Where to start?
    Fake it till you make it

    View Slide

  12. Fake it till you make it
    public function testNullCase()
    {
    $name = null;
    $invertedName = $this->nameInverter
    ->invert($name);
    self::assertEquals('', $invertedName);
    }
    public function invert($name)
    {
    // Forcing test to pass
    return ‘’;
    }
    TEST CODE PRODUCTION

    View Slide

  13. public function invert($name)
    {
    return 'Diego';
    }
    PRODUCTION
    Fake it till you make it
    public function testSingleName()
    {
    $name = 'Diego';
    $invertedName = $this->nameInverter
    ->invert($name);
    self::assertEquals(
    'Diego',
    $invertedName
    );
    }
    TEST CODE

    View Slide

  14. Fake it till you make it
    PRODUCTION
    public function invert($name)
    {
    if (!$name) {
    return '';
    }
    return $name;
    }

    View Slide

  15. Triangulation

    View Slide

  16. Triangulation
    class NameInverterTest extends TestCase
    {
    public function testNullCase()
    {
    $invertedName = $this->nameInverter->invert(null);
    self::assertEquals('', $invertedName);
    }
    public function testSingleName()
    {
    $invertedName = $this->nameInverter->invert('Diego');
    self::assertEquals('Diego', $invertedName);
    }
    }

    View Slide

  17. Fake it till you make it
    &
    Triangulation
    The Best of Friends

    View Slide

  18. Assert First

    View Slide

  19. Assert First
    class NameInverterTest extends TestCase
    {
    public function testFullName()
    {
    $name = 'Diego Aguiar';
    $invertedName = $this->nameInverter
    ->invert($name);
    self::assertEquals('Aguiar, Diego', $invertedName);
    }
    }

    View Slide

  20. Getting
    Stuck

    View Slide

  21. Why does
    it happen?
    ➢ It’s a symptom of a problem
    ➢ Production code is too specific
    ➢ You wrote the wrong test
    ➢ Aiming for the goal too fast

    View Slide

  22. How to get
    Unstuck
    ➢ Step up and think for a simpler test
    ➢ Refactor hard-to-test code
    ➢ Write a list of use-cases
    ➢ Ignore testing for a moment

    View Slide

  23. When Not
    to TDD
    ➢ Configuration
    ➢ Discovering
    ➢ Repository Queries

    View Slide

  24. ➢ TDD is more like a discipline
    ➢ There are techniques to get you unstuck
    ➢ TDD is a tool, not a rule
    Conclusion
    @MolloKhan

    View Slide

  25. Happy TDD!
    @MolloKhan
    “One test a day keeps bugs away"

    View Slide

  26. Z zero (or empty case)
    O one
    M many
    B boundaries
    I interfaces
    E exceptions
    BONUS!!
    https://i.ytimg.com/vi/0xRCYmbvfEs/maxresdefault.jpg

    View Slide

  27. Happy TDD!
    @MolloKhan
    THANK YOU AGAIN!

    View Slide