44
Test Driven Development and Related Techniques For Non-Developers ;-) IBM 2012 Peter Kofler, ‘Code Cop’ @codecopkofler www.code-cop.org Copyright Peter Kofler, licensed under CC-BY.

Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Test Driven Developmentand Related TechniquesFor Non-Developers ;-)

IBM 2012

Peter Kofler, ‘Code Cop’@codecopkofler

www.code-cop.org

Copyright Peter Kofler, licensed under CC-BY.

Page 2: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Peter Kofler

• Ph.D. (Appl. Math.)• (Java) Software Developer• with IBM since 1 year• SDM Costing, IGA, GBS

Page 3: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

The opinions expressed here are my own and do not necessarily represent

those of current or past employers.

Page 4: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Agenda

1) What does it look like?

2) Why is it important?

3) What you need to do!

4) Summary

Page 5: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

1) So what is it all about?

The Prime FactorsKata (Demo)

Page 6: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

What Exactly Will We Do Now?

● write code together● using TDD● see techniques

and patterns● discuss while doing

Page 7: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

The Requirements.

• Write a class named “PrimeFactors” that has one static method: generate.– The generate method takes an integer

argument and returns a List<Integer>.– That list contains the prime factors in

numerical sequence.

http://butunclebob.com/ArticleS.UncleBob.ThePrimeFactorsKata

Page 8: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

First Some Math.

● Prime Number: a natural number > 1 that has no divisors other than 1 and itself.

● e.g. 2, 3, 5, 61, 67, ..., 997, ..., 243112609-1, …● Prime Factors: the prime numbers that divide an integer exactly without remainder.

● e.g. 2 = 2, 4 = 2 * 2, 24 = 2 * 2 * 2 * 3288 = 25 * 32

Page 9: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

The Requirements (for Mortals).

• code a file/module/class “PrimeFactors”• code a function/method/routine

“generate”• accept an integer number as parameter• return the prime factors of that number

Page 10: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Demo(Code Kata)

Page 11: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Keep the bar green to keep the code clean.

Page 12: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Unit Testing

• test individual units• isolate each part• show that the individual parts are correct• regression testing• sort of living documentation• executed within a framework

http://en.wikipedia.org/wiki/Unit_testing

Page 13: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Test-Driven Development

• add a test• run all tests and see if the new one fails• write some code• run all tests and see them succeed• refactor code mercilessly• „Red Green Refactor“

http://en.wikipedia.org/wiki/Test_Driven_Development

Page 14: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

A minute ago all their code worked

Page 15: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Refactoring

Refactoring is a technique for restructuring

an existing body of code, altering its internal structure

without changing its external behavior.

(Martin Fowler)

Page 16: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Refactoring

• small behavior preserving transformations• sequence of transformations produce a

significant restructuring• each transformation is small, less likely to

go wrong• system is kept fully working after each

change• verified by working tests

Page 17: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external
Page 18: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Code Coverage

comprehensiveness of tests

Page 19: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Beware!

comprehensiveness quality!

Page 20: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Small Advice

Never measure developers by

Code Coverage!

Page 21: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Demo(Coverage in Eclipse)

Page 22: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Demo(Jenkins)

Page 23: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external
Page 24: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

ContinuousIntegration

Page 25: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Continuous Integration

• Maintain a code repository• Automate the build• Make the build self-testing• Everyone commits every day• Every commit should be built• Keep the build fast• Everyone can see the results of the build

http://en.wikipedia.org/wiki/Continuous_integration

Page 26: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Immediate Feedbackand

Early Warning

Page 27: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Demo(Jenkins)

Page 28: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

2) Why should we use TDD?

• Writing tests takes time

• and we need to deliver client value.

I don't have time.I don't have time.I don't have time.

Page 29: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Do you still use that one?

Page 30: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Discussion

• What do you think are the benefits of TDD?– rapid feedback– …– ...

Page 31: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Benefits

• speed up • coverage

– measurable validation of correctness• quality

– proven to save maintenance costs

• creates a detailed specification

Page 32: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Benefits

• improve design – drive the design of a program– think in exposed API

Page 33: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Problems

• missing management buy-in

• high ramp-up time• still need

integration tests

http://watirmelon.com/2012/01/31/introducing-the-software-testing-ice-cream-cone/

Page 34: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

TDD will not fix missing skills!

• badly written tests– brittle– blinking– slow– duplicate code– maintenance burden– wrong abstractions– not really unit tests

Page 35: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Because TDD is hard!

• need to know OO, design, abstraction, ...

• new style of thinking• need self-discipline• (learning curve)

Page 36: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

3) What you need to do

Page 37: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Problem Indicators: CI

• blinking builds• builds broken for long time• increasing build

execution time• decreasing code

coverage

Page 38: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Problem Indicators: Tests

• slow• fragile• no detailed feedback• “Ice-Cream Cone”

Page 39: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Problem Indicators: Developers

• claiming that they can't write tests• claiming that they can't fix tests• wanting to delete tests

Page 40: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

“Buzzwords” Summary

• Techniques– Unit Testing – Test Driven Development– Refactoring– Code Coverage– Continuous Integration

• Management Buy-In• Keep an eye on the CI server

Page 41: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Support TDD

Do not compromiseon techniques!

Page 42: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Thank You

Page 43: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

Peter Kofler

@codecopkofler

www.code-cop.org

Page 44: Test Driven Development and Related Techniques For Non ... · Test-Driven Development • add a test • run all tests and see if the new one fails ... without changing its external

CC Images• Drive: http://www.flickr.com/photos/hjem/367306587/

• Judge: http://www.flickr.com/photos/eldave/6169431454/

• List: http://www.flickr.com/photos/kylesteeddesign/3724074594/

• Question mark: http://www.flickr.com/photos/oberazzi/318947873/

• Fence: http://www.flickr.com/photos/30830597@N08/3630649274/

• Coverage: http://www.flickr.com/photos/paulk/3166328163/

• Works: http://www.codinghorror.com/blog/archives/000818.html

• Cash: http://www.flickr.com/photos/mindfire/315274981/

• Steep: http://www.flickr.com/photos/worldofoddy/229501642/

• Want You: http://www.flickr.com/photos/shutter/105497713/

• Warn: http://www.flickr.com/photos/hugosimmelink/2252095723/

• Questions: http://www.flickr.com/photos/seandreilinger/2326448445/