clear lines consulting · clear-lines.comsilicon valley code camp 2008 · nov 8, 2008 · 1...

28
Clear Lines Consulting · clear- lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 1 Test-Driven Development An introduction for C# developers

Upload: giles-fox

Post on 31-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 1

Test-Driven Development

An introduction for C# developers

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 2

Who I am

» Mathias Brandewinder– Background in business and quantitative analysis

methods– Develop in C#/.NET for the past 5 years– Started my own business since over a year

» www.clear-lines.com/blog

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 3

My goal for today

» I would like you to be able to start as you leave the room

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 4

Approach

» Interactive: write code live» Focus on C#» Focus primarily on NUnit– Covering also Visual Studio 2008 tools

» Assuming little or no background in TDD/unit testing

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 5

The plan

» What is TDD?» TDD in action: the basics– Using Nunit– Using VS2008

» Less basic TDD» Mocks» Beyond TDD» Questions

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 6

What is TDD?

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 7

Test-Driven Development

» Is a development methodology– Not a testing methodology

» Aims for “clean code that works, now”» Write the test first, then the code which

satisfies the test

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 8

Source

» Test-Driven Development By Example, Kent Beck (Addison Wesley)

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 9

The rules

» Write new code only if an automated test has failed

» Eliminate duplication

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 10

Red/Green/Refactor

» Red: write a little test that doesn’t work, and perhaps doesn’t even compile at first

» Green: make the test work quickly, committing whatever sins necessary in the process

» Refactor: eliminate all of the duplication created in merely getting the test to work

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 11

Benefits: the obvious

» Simple and predictable» Small steps» Fully tested code from the get-go» Very reassuring

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 12

TDD in action

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 13

It’s Monday morning, and…

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 14

Your mission, if you accept it

» Add a functionality to an existing application, to compute the distance between cities

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 15

A quick math reminder

» We will assume that “the world is flat”» Distance between 2 points:

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 16

Let’s get started!

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 17

What have we seen so far?

» [TestFixture], [Test], Assert» One test at a time» Write “as if” you had the feature done» Write tests as “sentences”» 3 patterns– Fake it ‘til you make it– Obvious implementation– Triangulate

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 18

What have we seen so far?

» Separate projects» Debugging a test

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 19

Less basic TDD

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 20

Some classic useful test techniques

» TestRow» Testing for exceptions» Setup & Teardown

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 21

Mocks

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 22

Mocks

» “Subject under test” vs. Collaborators » Mocks to simplify your life– “Classicists”: by default, use the real thing

» Mocks, the dark side of design patterns– “Mockists”: use Mocks in interactions

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 23

What have we seen so far?

» When testing the interaction of a class with another class, Mocks are a convenient way to “fake” the collaborator

» Focus on behavior and not state

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 24

Mocks and patterns

IStrategy

StrategyB StrategyA

MyClass

Strategy: IStrategy

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 25

Beyond TDD

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 26

A few questions

» What are the benefits of TDD?» Does TDD slow you down?» How much testing is enough?» Does TDD mean no upfront design?» What are good tests?» Are TDD people terrorists?

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 27

Questions?

Clear Lines Consulting · clear-lines.com Silicon Valley Code Camp 2008 · Nov 8, 2008 · 28

To contact me…

[email protected]