unit testing & tdd

25
Unit testing TDD XAVI HIDALGO · xavi.hidalgo @apiumtech.com www.apiumtech.com & @xavi_reload ed http://apiumtech.com/ blog/ http://es.linkedin.com/in/ xavireloaded/

Upload: xavi-hidalgo

Post on 28-Jul-2015

673 views

Category:

Presentations & Public Speaking


4 download

TRANSCRIPT

Page 1: Unit testing & tdd

Unit testingTDD

XAVI HIDALGO · [email protected]

&@xavi_reload

edhttp://apiumtech.com/blog/http://es.linkedin.com/in/

xavireloaded/

Page 2: Unit testing & tdd

Unit testingTDD &

@xavi_reloadedhttp://apiumtech.com/

blog/http://es.linkedin.com/in/xavireloaded/

XAVI HIDALGO · [email protected]

Page 3: Unit testing & tdd

Programmers are constantly in maintenance mode(The Pragmatic Programmer)

XAVI HIDALGO · [email protected]

www.apiumtech.com

A closed, fixed-requirements system is a programmers’

chimera:

● requirements always change on the way

(unexpectedly AND unpredictably)

● even if not, you always discover new requirements as you

dig into the system and the code. This implies change

● even if not, the system needs to be bug-fixed, which

implies change

Page 4: Unit testing & tdd

Design principles in agile

XAVI HIDALGO · [email protected]

www.apiumtech.com

DRY YAGNI KISS

Don’t repeat yourself You aren’t gonna need it Keep it simple stupid

Or equivalents:Say it Once and only onceChoose the Simplest strategy that could possibly work“keep it DRY, shy and tell the other guy” (from the authors of Pragmatic Programmer)

Emergent/evolutionary design

Page 5: Unit testing & tdd

Enabling Practices from XP

XAVI HIDALGO · [email protected]

www.apiumtech.com

“Enabling” = Practices that enable an evolutionary design when not used, the result of evolutionary design is always: Entropy

Engineering Practices (in order of application):● Unit Testing (allows:)● Refactoring● Automated Build● Continuous Integration● Continuous design● Emergent architecture● Pair Programming

Refactoring is forbidden without tests = high risk with none business value Without Unit Testing there’s not evolutionary design

Page 6: Unit testing & tdd

Agile learning steps(from Osherove)

XAVI HIDALGO · [email protected]

www.apiumtech.com

1. Learn Good Unit Testing

2. Learn Test Driven Development (TDD)

3. Learn S.O.L.I.D. and advanced OO Design principles

Meanwhile: remove duplication (be DRY!!)

Page 7: Unit testing & tdd

Unit Testing Economics

XAVI HIDALGO · [email protected]

www.apiumtech.com

UT and TDD are productivity development tools, NOT (only) quality or testing tools.

Page 8: Unit testing & tdd

TDD Principles

XAVI HIDALGO · [email protected]

www.apiumtech.com

GOALSClean Code that works, Now!

RULES1. Write new code only if an automated test has failed2. Eliminate duplication

PRACTICESmall cycles of /red/green/refactor

Page 9: Unit testing & tdd

A very simple development workflow

XAVI HIDALGO · [email protected]

www.apiumtech.com

#red#green

#refactor

Page 10: Unit testing & tdd

Unit Test Demo

XAVI HIDALGO · [email protected]

www.apiumtech.com

Hands on!

Page 11: Unit testing & tdd

Practice 1 (basic tests)

XAVI HIDALGO · [email protected]

www.apiumtech.com

Create a class called “Dog” Create a method called sayHello(String greeting) sayHello returns “wroof”+ greeting

● Make a test on the sayHello method● Use SetUp() Dropdown()● Use DataProviders● Use different Assertions

Page 12: Unit testing & tdd

Practice 2 (more complex tests)

XAVI HIDALGO · [email protected]

www.apiumtech.com

Create a class called “Petshop” Class petshop has a Dog PropertyCreate a method called saySomething() saySomething returns Dog.sayHello()

● Make a test on the saySomething method

● Use Stubs, Mocks and Fakes● Run the suite● Use different Assertions

Page 13: Unit testing & tdd

Unit Test Conclusions

XAVI HIDALGO · [email protected]

www.apiumtech.com

Did you seesomething

interesting ?

Page 14: Unit testing & tdd

How did we...

XAVI HIDALGO · [email protected]

www.apiumtech.com

Refactoring ?

Automated Build ?

Continuous Integration ?

Continuous design ?

Emergent architecture ?

Pair Programming ?

Page 15: Unit testing & tdd

Unit testingTDD

XAVI HIDALGO · [email protected]

&@xavi_reload

edhttp://apiumtech.com/blog/http://es.linkedin.com/in/

xavireloaded/

Page 16: Unit testing & tdd

The great questionAre UT sufficient to replace manual test?

XAVI HIDALGO · [email protected]

www.apiumtech.com

Manual test is still indispensable ● Human interactions and validation● Functional feedback● Integration tests difficult to automate● Manual test has a completely different role:● Remember: UT & TDD are design/develop tools, NOT

testing tools

Then what is the great gain? (see above and previous)

● All that you can validate automatically you do● Manual test is executed only few times – it’s the last

validation

In legacy code phase accurate Manual Tests are still needed

NO

Page 17: Unit testing & tdd

Test Driven Development Demo

XAVI HIDALGO · [email protected]

www.apiumtech.com

Hands on!

Page 18: Unit testing & tdd

Practice 1 (KATA)

XAVI HIDALGO · [email protected]

www.apiumtech.com

Page 19: Unit testing & tdd

Practice 1 (KATA)

XAVI HIDALGO · [email protected]

www.apiumtech.com

http://osherove.com/tdd-kata-1/

Page 20: Unit testing & tdd

Practice 1 (KATA) String Calculator

XAVI HIDALGO · [email protected]

www.apiumtech.com

1. Create a simple String calculator with a method int Add(string numbers)1. The method can take 0, 1 or 2 numbers, and will return their sum (for an

empty string it will return 0) for example “” or “1” or “1,2”2. Start with the simplest test case of an empty string and move to 1 and two

numbers3. Remember to solve things as simply as possible so that you force yourself to

write tests you did not think about4. Remember to refactor after each passing test

Page 21: Unit testing & tdd

Practice 1 (KATA) String Calculator

XAVI HIDALGO · [email protected]

www.apiumtech.com

1. Allow the Add method to handle an unknown amount of numbers

Page 22: Unit testing & tdd

Practice 1 (KATA) String Calculator

XAVI HIDALGO · [email protected]

www.apiumtech.com

1. Allow the Add method to handle new lines between numbers (instead of commas).1. the following input is ok: “1\n2,3” (will equal 6)2. the following input is NOT ok: “1,\n” (not need to prove it - just clarifying)3. Support different delimitersto change a delimiter, the beginning of the string will

contain a separate line that looks like this: “//[delimiter]\n[numbers…]” for example “//;\n1;2” should return three where the default delimiter is ‘;’ .

4. the first line is optional. all existing scenarios should still be supported

Page 23: Unit testing & tdd

Practice 1 (KATA) String Calculator

XAVI HIDALGO · [email protected]

www.apiumtech.com

1. Calling Add with a negative number will throw an exception “negatives not allowed” - and the negative that was passed.if there are multiple negatives, show all of them in the exception message

Page 24: Unit testing & tdd

TDD Conclusions

XAVI HIDALGO · [email protected]

www.apiumtech.com

Did you seesomething

interesting ?

Page 25: Unit testing & tdd

The End

XAVI HIDALGO · [email protected]

www.apiumtech.com

ESSENTIAL BIBLIOGRAPHY

[Osherove, R., The Art of Unit Testing, 2009][Meszaros, G., xUnit Test Patterns, 2007][Beck, K., Test Driven Development by Example, 2002][Fowler, M. Beck, K., Refactoring, 1999][Osherove, R., Iserializable – Roy Osherove Blog, http://weblogs.asp.net/rosherove/][Meszaros, G., http://xunitpatterns.com/][Kniberg, H., Scrum and Xp from the trenches, 2007][Succi et al., eXtreme Programming eXamined, 2001][Feathers, C., Working Effectively with Legacy Code, 2004][Hunt, A. et al., The Pragmatic Programmer, 1999]