Skip to content

Instantly share code, notes, and snippets.

@giantryansaul
Last active May 8, 2018 15:59
Show Gist options
  • Save giantryansaul/d7dde50cef1f8d52224f6dace9c704db to your computer and use it in GitHub Desktop.
Save giantryansaul/d7dde50cef1f8d52224f6dace9c704db to your computer and use it in GitHub Desktop.
Test Driven Development

Test Driven Development

(TDD)

What is it?

  • Write tests before you write code
  • Tests drive the code structure

A Brief History

Kent Beck

The original description of TDD was in an ancient book about programming.

Kent Beck

It said you take the input tape, manually type in the output tape you expect, then program until the actual output tape matches the expected output.

Extreme Programming

(1995-ish)

Test-Driven Development:

By Example

2002

https://i.imgur.com/RwLbz9n.png

Behavior Driven Development

(BDD)

2009

Is TDD Dead?

2014 Debate

https://i.imgur.com/RxCSYUI.png

David Heinemeier Hansson

I don't know how/what to test before I've seen the program.

Kent Beck

I have no business programming something before I know how to test it.

How to do it

Red Green Refactor

https://jfiaffe.files.wordpress.com/2014/09/redgreenrefacor.png

Red Green Refactor

  1. Write a failing test
  2. Make the test pass
  3. Clean-up code
  4. Repeat until desired logic
[Fact]
public void TestMyStack()
{
    var example = new Example();
    var result = example.Add(1);
    Assert.Equal(1, result);
}
class Example
{
    public int Store = 0;
    internal int Add(int i)
    {
        throw new NotImplementedException();
    }
}

Fail!

class Example
{
    public int Store = 0;
    internal int Add(int i)
    {
        Store = Store + i;
        return Store;
    }
}

Pass!

class Example
{
    public int Store = 0;
    internal bool Add(int i)
    {
        return Store + i; // inline
    }
}

Refactor!

Repeat!

...until you're done

Benefits

  • Tests are already created
  • Drives great architecture
  • Easier to change
  • Lets you known when you are "done"

Limitations

  • Tests and code are only as good as what the developer anticipates

  • Changes to the code require more changes to the unit tests

  • Longer time spent on single feature

But like anything, this improves with practice

Great for "greenfield" code

https://wallpapercave.com/wp/hN0TnNU.jpg

But what about "legacy" code?

Michal Feathers

Legacy Code is Code Without Tests

The Problem: Tight coupling

https://i.imgur.com/1HqD70B.png

Loose Coupling

https://i.imgur.com/WVQ8QAB.png

Tight Coupling

https://i.imgur.com/hBHGqc8.png

TDD to the rescue!

  • Tests drive loose coupling
  • Write tests you'd expect to pass
  • Break up code using "safe refactors"
  • Get your classes out of tight dependencies

Avoid heavy mocking!

use interfaces when possible

Behavior Driven Development

(BDD)

BDD focuses on tests which describe behavior. Rather than single units of code.

System Testing vs Unit Testing

Feature: search Wikipedia
  Scenario: direct search article
    Given Enter search term 'Cucumber'
    When Do search
    Then Single result is shown for 'Cucumber'

Summary

  • Test Driven Development
  • Red Green Refactor
  • Better, more changable code
  • Trade-off in time spent developing

Summary

  • Great for greenfield AND legacy code!
  • Aim for loosely-coupled code
  • Avoid heavy mocking!
  • Look at BDD for a TDD approach to E2E

Questions?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment