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

More Decks by Diego Aguiar

Other Decks in Programming

Transcript

  1. ➢ Developer at SymfonyCasts ➢ Hiking ➢ Gaming (Warcraft FTW!)

    ➢ Tepic, Mexico Diego Aguiar Beltrán @MolloKhan
  2. Main Topics ➢ What TDD Really is ➢ Useful TDD

    Techniques ➢ What “Getting Stuck” means ➢ When Not to TDD
  3. What is TDD? ➢ Programming Methodology ➢ It’s Composed of

    3 Laws ➢ Tests Always Go First ➢ Red, Green, Refactor cycle
  4. A bit of History… ➢ Kent Beck inventor of TDD

    ➢ Invented In the late 1990’s ➢ As a way to write better quality code
  5. TDD Techniques ➢ Fake it till you make it ➢

    Triangulation ➢ Assert First
  6. 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
  7. 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
  8. Fake it till you make it PRODUCTION public function invert($name)

    { if (!$name) { return ''; } return $name; }
  9. 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); } }
  10. Assert First class NameInverterTest extends TestCase { public function testFullName()

    { $name = 'Diego Aguiar'; $invertedName = $this->nameInverter ->invert($name); self::assertEquals('Aguiar, Diego', $invertedName); } }
  11. 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
  12. 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
  13. ➢ TDD is more like a discipline ➢ There are

    techniques to get you unstuck ➢ TDD is a tool, not a rule Conclusion @MolloKhan
  14. Z zero (or empty case) O one M many B

    boundaries I interfaces E exceptions BONUS!! https://i.ytimg.com/vi/0xRCYmbvfEs/maxresdefault.jpg