Skip to content

Latest commit

 

History

History
368 lines (255 loc) · 9.36 KB

intro-tdd.md

File metadata and controls

368 lines (255 loc) · 9.36 KB
title author date css
Introduction into test-driven development
Zeger Hendrikse
2023-09-29
css/custom.css

Coders should test — testers should code

Zeger Hendrikse

 

 

 

 

 

We all should do TDD!


Me, myself, and I

Zeger


eXtreme Programming

XP


Feedback loops


Dave Farley — the culture of TDD

TDD


Daniel North and Chris Matts

  • Test Suite => specification
  • Test => scenario
  • Structure tests around "Given, When, Then"

rSPec

# spec/string_calculator_spec.rb
describe StringCalculator do

  describe ".add" do
    context "given an empty string" do
      it "returns zero" do
        expect(StringCalculator.add("")).to eq(0)
      end
    end
  end
end

We test behaviour with TDD

<iframe width="100%" height="500" src="//jsfiddle.net/zhendrikse/bu7tv1kp/3/embedded/js,result/dark/" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0">

Specifications drive design

Design


TDD & BDD

(c) Lasse Koskela


Contra-variance and co-variance

Contra- and co-variance


unit tests


The structure of the tests must not reflect the structure of the production code because that much coupling makes the system fragile and obstructs refactoring. Rather, the structure of the tests must be independently designed so as to minimize the coupling to the production code.


Contra-variance is only achieved using TDD

Why? ==> Because we specify!


Rulez of the TDD game

Red Green Refactor
  1. Write a failing test
  2. Make it pass
  3. Refactor relentlessly

Martin Fowler: refactoring

Martin Fowler

... is a disciplined technique for restructuring an existing body of code, altering its internal structure without changing its external behavior

Rulez of the TDD game

Small increments

We are not allowed to write:

  1. any production code before you have a failing test
  2. any more of a test than is sufficient to fail (also compilation!)
  3. any more code than is sufficient to pass the one failing unit test

Kent Beck

Kent Beck


  1. Passes the tests
  2. Reveals intention (Clean code) → game of life:
(defn next-generation-of [game]
  (map #(to-living-cell 
         (which-both 
          is-dead? 
            (has-exactly-three? (living-neighbours-in game))) %) 
  (map #(to-dead-cell 
         (which-both 
          is-alive? 
          (which-either 
           (has-less-than-two? (living-neighbours-in game)) 
           (has-more-than-three? (living-neighbours-in game)))) %) game)))
  1. No duplication (DRY)
  2. Fewest elements (STTCPW)

TDD is not building a bridge nor house!

cartoon


User story

As a worker in a restaurant

I want to place my clean plates on a stack

so that I always have plates available to serve dishes


Plans are worthless ...

... but planning is essential:

  • Start with an empty stack
  • Define pop on an empty stack
  • Define push on an empty stack
  • Define pop on a non-empty stack
  • Define multiple pushes and pops

 

Credits to Kent Beck and Eisenhower!


<iframe width="100%" height="700" src="//replit.com/@zwh/Scrumblr?embed=true" allowfullscreen="allowfullscreen" allowpaymentrequest frameborder="0">

Let's do this


Retrospective


It's only the beginning...


Resources

With special thanks to ...

Uncle Bob


Goals

Coding + testing are the same activity

The importance of test contra-variance

See how TDD is done in practice

Motivation to learn & practice more TDD


Rulez during this session

  • Questions are allowed at all times
  • The goal is to illustrate the TDD process
    • The goal is not to write the best
      • Javascript ever
      • Python ever
      • ...
    • User story is not the most realistic either


  • Summary

    • Unit test === Functional test
    • Unit tests test the smallest functional unit
    • Practicing TDD/BDD  ==>  test contra-variance
    • xUnit tests ≠ TDD
    • BDD ≠ tools (Cucumber / Specflow)
    • BDD = RSpec-style specifications
      (Jasmine/Mocha/Jest/Specnaz/Mamba...)
    • Unit test === Functional test