real developers-dont-need-unit-tests

37
Real Developers Don’t Need Unit Tests Mythbusting TDD

Upload: wakaleo-consulting

Post on 11-May-2015

3.586 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Real developers-dont-need-unit-tests

Real Developers Don’t Need Unit Tests

Mythbusting TDD

Page 2: Real developers-dont-need-unit-tests

John Ferguson SmartCEO Wakaleo Consulting

Consultant, Trainer, Mentor, Author,...

So who is this guy, anyway?

Page 3: Real developers-dont-need-unit-tests

John Ferguson SmartCEO Wakaleo Consulting

Consultant, Trainer, Mentor, Author,...

So who is this guy, anyway?

Page 4: Real developers-dont-need-unit-tests

Java Power Tools Bootcamp

So who is this guy, anyway?

LondonJanuary 24-28 2011

maven

JUnitHudson

Page 5: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

Chuck Norris’s code doesn’t have bugs

His code always work. ALWAYS.

Bugs?

Page 6: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

Chuck Norris’s code is perfectly designed the first time round

His favorite design pattern is the Roundhouse Kick

Bugs?Emergent design?

Page 7: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

Chuck Norris doesn’t need technical documentation

He just stares down the code until it tells him everything he wants to know

Bugs?Emergent design?

Technical documentation?

Page 8: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

Chuck Norris doesn’t need requirements to be executable

He can execute whatever he wants

Bugs?Emergent design?

Technical documentation?Executable requirements?

Page 9: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

Chuck Norris doesn’t need acceptance tests

Everything Chuck does is acceptable

Bugs?Emergent design?

Technical documentation?Executable requirements?

Acceptance Tests?

Page 10: Real developers-dont-need-unit-tests

Introduction

Real Developers Don’t Need Unit Tests

OK, so Chuck Norris doesn’t need Unit Tests

But what about the rest of us?

Page 11: Real developers-dont-need-unit-tests

TDD Basics

TDD is not about writing tests

So what is this TDD thing, anyway?

Page 12: Real developers-dont-need-unit-tests

TDD in a nutshell...

TDD Basics

“Red...Green...Refactor”

Page 13: Real developers-dont-need-unit-tests

TDD in a nutshell...

TDD Basics

“The code your code could smell like”

Page 14: Real developers-dont-need-unit-tests

TDD in a nutshell...

TDD Basics

Always tidy up after you play...

Page 15: Real developers-dont-need-unit-tests

Stay focused on the requirements

Stay On Target

User Story 1 - Transfer funds

As a bank clientI want to withdraw funds from my current accountSo that I can buy beer

User Story 1 - Acceptance Tests

- Cash withdrawal should deduct sum from balance- Client cannot withdraw if insufficient funds- Client cannot withdraw if account blocked... @Test public void aCashWithdrawalShouldDeductSumFromBalance() { Account account = new Account(); account.makeDeposit(100); account.makeCashWithdraw(60); assertThat(account.getBalance(), is(40)); }

Page 16: Real developers-dont-need-unit-tests

User Story 1 - Transfer funds

As a bank clientI want to withdraw funds from my current accountSo that I can buy beer

User Story 1 - Acceptance Tests

- Cash withdrawal should deduct sum from balance- Client cannot withdraw if insufficient funds- Client cannot withdraw if account blocked...

Good tests are ‘executable requirements’

Executable Requirements

@Test public void aCashWithdrawalShouldDeductSumFromBalance() { Account account = new Account(); account.makeDeposit(100); account.makeCashWithdraw(60); assertThat(account.getBalance(), is(40)); }

Page 17: Real developers-dont-need-unit-tests

User Story 1 - Transfer funds

As a bank clientI want to withdraw funds from my current accountSo that I can buy beer

User Story 1 - Acceptance Tests

- Cash withdrawal should deduct sum from balance- Client cannot withdraw if insufficient funds- Client cannot withdraw if account blocked...

Know what requirements are done

Know where you’re at

Page 18: Real developers-dont-need-unit-tests

Refactor with confidence

Safety net

Page 19: Real developers-dont-need-unit-tests

Testable code is well-designed code

Better-designed code

Page 20: Real developers-dont-need-unit-tests

Improve your legacy code base

Not just for green fields

Page 21: Real developers-dont-need-unit-tests

TDD for unbelievers

You still have to use your brainAlso use other design activities (domain modeling, DDD,...)

TDD is not applicable everywhereHighly visual coding, ‘know it when you see it’ stuff,...

TDD works best if you have a vision

So TDD is the answer to all my problems?

No, sorry, TDD is not a Silver Bullet

Page 22: Real developers-dont-need-unit-tests

TDD for unbelievers

But how can I write tests if I don’t know what the code looks like?

How can you write code when you can’t express what it should do?

“Write code for others as you would have them write code for you”

- Some agile dude

Page 23: Real developers-dont-need-unit-tests

TDD for unbelievers

New TDDers will take maybe 20-30% longer

Experienced TDD developers don’t spend much (if any) more time coding

But the code is of much higher quality

I don’t have time to write tests

But you have time to fix the code afterwards, right?

“I can get it done much faster if it doesn’t have to work”

- Kent Beck (paraphrased)

Page 24: Real developers-dont-need-unit-tests

TDD for unbelievers

You will write lots of tests......but your code will be more focused and more accurate

But I'm writing more test code than application code!

Sure, writing tests is part of the process

Page 25: Real developers-dont-need-unit-tests

TDD for unbelievers

Validate behaviour, don’t verify implementation

My tests break whenever I change my code

Try to test the behaviour, not the implementation

Page 26: Real developers-dont-need-unit-tests

TDD for unbelievers

Be wary of test maintenance overhead

Test code is not second-class code

Refactor to keep your test code maintainable

Avoid technical debt

Our tests are too hard and take too much time to keep up to date

Treat your test code as production code

Page 27: Real developers-dont-need-unit-tests

TDD for unbelievers

You still need your testers!

You can still make mistakes...

...but they are easier to isolate and to fix

If you find a bug, just add another test to reproduce it, then fix it!

I found a bug in the production code. TDD doesn’t work.

see “TDD is not a Silver Bullet”

Page 28: Real developers-dont-need-unit-tests

TDD for unbelievers

TDD does not preclude initial high-level design

Brainstorm a high-level design approach

Define a common well-known domain language

Use an ‘Iteration 0’ and spikes

Don’t be afraid to refactor

TDD works better when you have a vision

I’ve heard TDD encourages sloppy design. Isn't it better to have a general vision of the problem before coding the solution?"

Sometimes, yes. That's what the refactoring phase is for.

Page 29: Real developers-dont-need-unit-tests

TDD for unbelievers

Unit tests when you can, integrations tests when you must

Integration tests for system boundaries

Mock out these boundary classes for the rest of your tests

I use a database/network/web service... and TDD-style unit tests can't test it. TDD doesn't work

You will need integration tests too

Page 30: Real developers-dont-need-unit-tests

TDD for unbelievers

Acceptance-Test Driven Development is about communication

BDD help BAs and POs participate in writing executable requirements

Our BAs and product owners aren’t buying this TDD stuff

Try putting them into the feedback loop

scenario "A cell with less than 2 live neighbours should die", { given "Given a cell with no live neighbours", when "a new generation is generated", then "that cell will have died"}

Page 31: Real developers-dont-need-unit-tests

TDD for unbelievers

Acceptance-Test Driven Development is about communication

BDD help BAs and POs participate in writing executable requirements

Our BAs and product owners aren’t buying this TDD stuff

Try putting them into the feedback loop

scenario "A cell with less than 2 live neighbours should die", { given "Given a cell with no live neighbours", { initialGrid = ""... .*. ..."" theUniverse = new Universe(seededWith(initialGrid)); } when "a new generation is generated", { theUniverse.createNextGeneration() } then "that cell will have died", { theUniverse.grid.shouldBe ""... ... ..."" }}

Page 32: Real developers-dont-need-unit-tests

TDD for unbelievers

Acceptance-Test Driven Development is about communication

BDD help BAs and POs participate in writing executable requirements

Our BAs and product owners aren’t buying this TDD stuff

Try putting them into the feedback loop

Given a living cell with no live neighbours like this....*....When a new generation is generatedthen the grid should look like this:.........

Page 33: Real developers-dont-need-unit-tests

TDD for unbelievers

Many BDD tools also have great reporting features

Test results summary

Failed stories

Unimplemented stories

Page 34: Real developers-dont-need-unit-tests

TDD for unbelievers

Many BDD tools have great reporting features

Test results summary

Test failure details

Unimplemented stories

Page 35: Real developers-dont-need-unit-tests

TDD for unbelievers

Unit tests when you can, integrations tests when you must

Learn about TDD - read books on TDD practices

Do pair program with someone who knows TDD

Participate in and organize coding dojos

<blatant plug>Training/mentoring</blatant plug>

“I’ve tried TDD, but I soon lapse back into my old code-first habits”

TDD takes practice and know-how

Page 36: Real developers-dont-need-unit-tests

Conclusion

Real Developers Don’t Need Unit Tests?

So maybe Chuck should be writing unit tests after all...

I agree. You go talk to him

Page 37: Real developers-dont-need-unit-tests

Enjoy your quiche!

John Ferguson SmartWakaleo Consulting Ltd.

http://www.wakaleo.comEmail: [email protected]

Twitter: wakaleo