TDD All the Things!

112
TDD All the Things! Liz Keogh @lunivore http:// lizkeogh.com

Transcript of TDD All the Things!

TDDAll the Things!

Liz Keogh@lunivorehttp://lizkeogh.com

An Introduction to TDD

How to TDD People!

An Introduction to Cynefin

Refactoring People and Life

Test-Driven Experiments

An Introduction to TDD

Write afailingtest

Make it passRefactor

New behaviour

public bool WaitFor(AutomationElementWrapper element, SomethingToWaitFor check, TimeSpan timeout, FailureToHappenHandler failureHandler, IEnumerable<AutomationEventWrapper> events) { Monitor.Enter(_waitingRoom); _triggeringEvent = null;

DateTime started = DateTime.Now; var handlerRemovers = AddPulsingHandlers(events, element);

bool checkPassed = true; while (!check(element, _triggeringEvent) && DateTime.Now.Subtract(started).CompareTo(timeout) < 0) { checkPassed = false; Monitor.Wait(_waitingRoom, timeout); } Monitor.Exit(_waitingRoom); ClearPulsingHandlers(handlerRemovers);

if (!checkPassed && !check(element, null)) { failureHandler(element); return false; } return true; }

public void ShouldWaitForEventsToOccur() { // Given an automation element _window = LaunchPetShopWindow(); var combo = _window.Find<ComboBox>("petFoodInput");

// When we cause a slow event on that element new Thread(() => { Thread.Sleep(200); combo.Select("PetFood[Carnivorous]"); }).Start();

// And we wait for the event var eventOccurred = false; new Waiter().WaitFor( combo, (src, e) => { eventOccurred = true; return combo.Selection.Equals("PetFood[Carnivorous]"); }, new TimeSpan(0, 0, 1), (ex) => Assert.Fail(), new List<AutomationEventWrapper> { new StructureChangeEvent(TreeScope.Element)});

// Then we should be notified when the event occurs Assert.IsTrue(eventOccurred); }

public void ShouldWaitForEventsToOccur(){

// Given an automation element _window = LaunchPetShopWindow();var combo = _window.Find<ComboBox>("petFoodInput");

// When we cause a slow event on that element new Thread(() => { Thread.Sleep(200); combo.Select("PetFood[Carnivorous]"); }).Start();

// And we wait for the eventvar eventOccurred = false;new Waiter().WaitFor(combo, (src, e) => { eventOccurred = true; return combo.Selection.Equals( "PetFood[Carnivorous]"); }, new TimeSpan(0, 0, 1), (ex) => Assert.Fail(), new List<AutomationEventWrapper> { new StructureChangeEvent( TreeScope.Element)});

// Then we should be notified// when the event occursAssert.IsTrue(eventOccurred);

}

ShouldWaitForEventsToOccur

Given an automation elementWhen we cause a slow event on that elementAnd we wait for the eventThen we should be notified when the event occurs.

Examples

Given a contextWhen an event happens

Then an outcome should occur

Arrange

Act

Assert

Given a contextWhen an event happens

Then an outcome should occur

Arrange

Given a contextWhen an event happens

Then an outcome should occur

Act

Given a contextWhen an event happens

Then an outcome should occur

Assert

An Example of an Example

Given Fred has bought a microwaveAnd the microwave cost £100

When we refund the microwaveThen Fred should be refunded £100.

With Pixies!

Given Fred has bought a microwaveAnd the microwave cost £100

When we refund the microwaveThen Fred should be refunded £100.

Let’s TDD a non-software process!

How to TDD People!

Let’s TDD a person!

Write afailingtest

New behaviour

Feedback

Given a contextWhen an event happens

Then an outcome should occur

Context inwhich theyact

Feedback

Given a contextWhen an event happens

Then an outcome should occur

Action they take

Feedback

Given a contextWhen an event happens

Then an outcome should occur

Outcomes

Refactor

Existing behaviour

and anchorwhat you value!

Number 1 rule offeedback:

Anchor what you value!

changing

behaviour

Write afailingtest

Refactor

Existing behaviour

and anchorwhat you value!

New behaviour

Write afailingtest

Refactor

Existing behaviour

and anchorwhat you value!

Make it pass

New behaviour

Describedesired

behaviour

Changethe

behaviour

The sandwich model

Start with something good

Say something bad

Finish with something good

The sandwich model done right

Anchor what you value

Describe desired behaviour

THEN change the behaviour

(People can do this bit themselves!)

An Introduction to Cynefin

The Innovation Cycle

CommoditiesDifferentiator

s Repeatability

Spoilers

Cynefin

Obvious

ComplicatedComplex

Chaotic

With thanks toDavid Snowden and Cognitive Edge

Cynefin

With thanks toDavid Snowden and Cognitive Edge

Disorder

Cynefin

Obvious

Chaotic

With thanks toDavid Snowden and Cognitive Edge

Estimating Complexity

5. Nobody has ever done it before4. Someone outside the org has done it before (probably a competitor)3. Someone in the company has done it before2. Someone in the team has done it before1. We all know how to do it.

Estimating Complexity

5 4 3

2

1

Refactoring Peopleand Life

Refactoring code

Make it run

Get the thing

that’s new working

Make it right

Make itfast

Separate concerns

so you can do more!

Good code

Focus on responsibilities and strengths

Is generous on input, strict on output

Is easy to understand and work with

You can trust clean code.

Dan North’s “Three Ages”

ExploreOptimise for

Discovery

StabilizeOptimize forRepeatability

CommoditizeOptimize for

Efficiency

Refactoring at scale

Horizon 3

Horizon 2

Horizon1

Refactoring people

Try it out

Get the thing

that’s new working

Focus on strengths

Use others for the boring

stuff!

Separate concerns

so you cando more!

Good people

Focus on responsibilities and strengths

Are generous in listening, honest in speaking

Are easy to understand and work with

You can trust good people.

Horizon 2fights for budgetwith Horizon 1.

Focusing on strengthsfights for time

with the boring stuff.

Trust other people’s code.Trust other people.

Let’s TDD a people process!

Let’s TDD a people process(Thanks Tobias!)

Given my son’s room is a messWhen I ask him to clean it up

Then the room should be clean.

SMART criteria

SpecificMeasurableAchievableRelevant

Timebound

Well-formed outcomes

SensationVision

HearingSmellTaste

Well-formed outcomes

Given my son’s room is a messWhen I ask him to clean it up

Then the room should be clean.

I can see the carpet;

no clothes on the

floor; no toys on the

floor.

I look at it and say,

Wow!

GROW

Goal

Reality

Options

Way forward

GROW

Given a contextWhen an event happens

Then an outcome should occur

Goal

GROW

Given a contextWhen an event happens

Then an outcome should occur

Reality

GROW

Given a contextWhen an event happens

Then an outcome should occur

Options / Way Forward

Life doesn’t havebugs,just

apologies

Real Options

Optionshave value

Optionsexpire

Never commit earlyunless you know why

BDD

Context

Event

Outcome

Test

Deliberate Discovery

A Naïve Scenario

CONTEXT EVENT OUTCOME

Well-formedoutcome

Event whichleadsto the

outcome

Reality.It’s all good,

right?

A Naïve Scenario

Given my son’s room is a messWhen I ask him to clean it up

Then the room should be clean.

Tom Gilb’s “Mafia Offer”

Given my son’s room is a messWhen I ask him to clean it up

Then the room should be cleanAnd he looks really miserable

and hates you?

Reality.It’s all good,

right?

Unwanted context

CONTEXT EVENT OUTCOME

Well-formedoutcome

Event whichleadsto the

outcome

UnwantedContext

UnwantedOutcome!

UnwantedContext

Subgoals

CONTEXT EVENT OUTCOME

Well-formedoutcome

Event whichleadsto the

outcome

Replacementcontext

UnwantedOutcome!

Given Scenario

CONTEXT EVENT OUTCOME

Well-formedoutcome

Event whichleadsto the

outcome

UnwantedContext

Replacementcontext

EVENT OUTCOME

Event whichleads to

replacement

Replacing the “Mafia Offer”

Given my son knowswhat “clean” looks like

And that it will make me say “Wow!”And be impressed with him

Given my son’s room is a messWhen I ask him to clean it up

Then the room should be cleanAnd he should be happy!

Changing reality

CONTEXT EVENT OUTCOME

Well-formedoutcome

Event whichleadsto the

outcome

UnwantedContext

Replacementcontext

EVENT

Event whichleadsto the

replacement

CONTEXT

Test-Driven Experiments

Cynefin

Obvious

ComplicatedComplex

Chaotic

With thanks toDavid Snowden and Cognitive Edge

SenseCategorizeRespond

SenseAnalyzeRespond

ProbeSense

Respond

ActSense

Respond

Whenever we do anythingnew

we will makediscoveries

CynefinTryingthings

out Probe

Experiment

How to use scenarios…

…in this space.

Deliberate Discovery

Assume ignorance

Assume second order ignorance

Optimize for discovery

Real Options

Options have value

Options expire

Never commit earlyunless you know why

The mostflawed commitments

we makeare the onesin our heads

A Safe-To-Fail Probe has…

A way of knowing it’s succeedingA way of knowing it’s failing

A way of dampening itA way of amplifying it

Coherence

Coherence

A reason for thinking thatthe probe might have

a positive impact

Can you think ofa scenario

in which this experimentsucceeds?

Success Scenarios

Given my client has a problemWhen I suggest a practice to help

Then they should be able to deliver software.

Success Scenarios

Given Fred has signed up for AppytasticWhen we sync to Facebook by default

Then Fred’s friends should seehow awesome Appytastic is!

Complementary Success Scenarios

Given Fred has signed up for AppytasticWhen we sync to Facebook by default

Then Fred should realise that Appytasticis a really easy way to get content

onto Facebook!

Conflicting Success Scenarios!

Given my client has a problemWhen I suggest a practice to help

Then it should help them to think ofdifferent practices

that really are helpful!

Failure Scenarios

Given my client has a problemAnd context I don’t know about

When I suggest a practice to helpThen they’ll think I’m an idiot.

Failure Scenarios

Given Fred has signed up for AppytasticWhen we sync to Facebook by default

Then Fred and Fred’s friendsmight see that Appytastic has

massive privacy issues.

A way of knowing it’s succeeding

My client delivers software.

Fred’s friends see how awesome Appytastic is (and sign up).

Success Metrics!

Software is delivered – how quickly?

Sign-up rate increases – how much does it increase by?

A way of knowing it’s failing

My client thinks I’m an idiot.

Fred and Fred’s friendsmight see that Appytastic has

massive privacy issues.

Failure metrics!

Frowning faces!(but you get what you measure,

so measure smiles)

Sign-up rate decreases;proportion of positive tweets decreases

What if our people

don’t have the

skills?

What ifthe 3rd party systemdoesn’t work that way?

What if someone

gives away

the admin

password?What if AWS fails?

What if the releasehas a bug in it?

What if a meteorhits our data centre?

A Safe-To-Fail Probe has…

A way of knowing it’s succeedingA way of knowing it’s failing

A way of dampening itA way of amplifying it

Coherence A way of avoiding failure completely

Success is guaranteed…

…when you’ve done it before…in the same context…with the same requirements…and the same people…and the same technology…and the same market…and you own a time machine.

Make it cheap to fail.

Given my client has a problemAnd context I don’t know about

When I suggest a practice to helpThen they’ll think I’m an idiot.

(Tell them it’s a way ofgetting context quickly.)

Make it cheap to fail

Given Fred has signed up for AppytasticWhen we sync to Facebook by default

Then Fred and Fred’s friendsmight see that Appytastic has

massive privacy issues!

(Put the flag on the server.)

Estimating Complexity

5. Nobody has ever done it before4. Someone outside the org has done it before (probably a competitor)3. Someone in the company has done it before2. Someone in the team has done it before1. We all know how to do it.

Estimating Complexity5 4 3

2

1

ComplicatedComplex

Obvious

Familiarity5 4 3

2

1

Experts familiarwith scenariosUnfamiliar

scenarios

Familiarscenarios

Disaster movies!

Context Questioning

Is there a context which,for some event,will give us more

options?

Context Questioning

Is there a context which,for some event,

will give outcomes that arecheaper to change?

Warning!

Be careful when you’reopening up options

that you’re notsignalling intent

The Shallow Dive into Chaos

ScenariosAnchor the behaviour you value.

In unfamiliar scenarios, create options –make it safe-to-fail.

Change code;help people change themselves.

Some experimentsshould fail.

Liz Keoghhttp://lizkeogh.com@lunivore