Introduction to test_driven_development

71
Introduction to Test-Driven Development Hao-Cheng Lee 本著作係依據創用 CC Attribution-ShareAlike 3.0 Unported 授權 條款進行授權。如欲瀏覽本授權條款之副本,請造訪 http: //creativecommons.org/licenses/by-sa/3.0/ ,或寄信至 Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA 。

description

This is the slides of my talk @ Taiwan Java User Group on 2011/07/09. You can watch the video here (in Chinese): http://vimeo.com/26538602

Transcript of Introduction to test_driven_development

Page 1: Introduction to test_driven_development

Introduction to Test-Driven Development

Hao-Cheng Lee

本著作係依據創用 CC Attribution-ShareAlike 3.0 Unported 授權條款進行授權。如欲瀏覽本授權條款之副本,請造訪 http:

//creativecommons.org/licenses/by-sa/3.0/ ,或寄信至 Creative Commons, 171 Second Street, Suite 300, San Francisco, California,

94105, USA 。

Page 2: Introduction to test_driven_development

About me

● Hao-Cheng Lee● Java Engineer for 7+ years● Software Engineer @ Armorize● Interested in Java, Scala, TDD, CI● email: [email protected]● twitter: haocheng● Google+: 浩誠

Page 4: Introduction to test_driven_development

Agenda

● What is TDD?

● Why use TDD?

● How to do TDD?

● Pros and Cons

● How do I start?

Page 5: Introduction to test_driven_development

TDD =

TFD +

Refactoring

Page 6: Introduction to test_driven_development
Page 7: Introduction to test_driven_development

Not about writing tests...

Page 8: Introduction to test_driven_development

TDD is about writing better

code

Page 9: Introduction to test_driven_development

from http://ch.englishbaby.com/forum/LifeTalk/thread/441379

Page 10: Introduction to test_driven_development

Test Myths

Page 11: Introduction to test_driven_development

I have no time for testing

Page 12: Introduction to test_driven_development

Technical Debt

from http://www.freedebtadvice-uk.com/

Page 13: Introduction to test_driven_development

My code is BUG-FREE!

Page 14: Introduction to test_driven_development

from http://vincentshy.pixnet.net/blog/post/5397455

Page 15: Introduction to test_driven_development

QA will do the testing

Page 16: Introduction to test_driven_development

Black Box Testing

from http://www.jasonser.com/marketing-black-box/

Page 17: Introduction to test_driven_development

http://www.ocoee.org/Departments/HR/

Page 18: Introduction to test_driven_development

Faster

from http://cllctr.com/view/c2fdb4d2625e109069c843ea1bb99e50

Page 19: Introduction to test_driven_development

from Test Driven Development Tutorial by Kirrily Robert

Page 20: Introduction to test_driven_development

Faster

● Shorter release cycle

● Automation saves time

● Find bugs earlier

Page 21: Introduction to test_driven_development

Better

from http://www.nataliedee.com/archives/2005/sep/

Page 22: Introduction to test_driven_development

Better

● Greater code coverage

● The courage to refactor

● Prevent regression bugs

● Improve your design

Page 23: Introduction to test_driven_development

"I'm not a great programmer; I'm just a good programmer with great habits." - Kent Beck

Page 24: Introduction to test_driven_development

Testing the Old Way

Page 25: Introduction to test_driven_development

TDD

Page 26: Introduction to test_driven_development

TDD

Page 27: Introduction to test_driven_development

How to do TDD?

● Design: figure out what you want to do● Test: write a test to express the design

○ It should FAIL● Implement: write the code● Test again

○ It should PASS

Page 28: Introduction to test_driven_development

Design

We need a method add(), which takes two parameters and add

them together, then it will return the result.

Page 29: Introduction to test_driven_development

Test

Page 30: Introduction to test_driven_development

java.lang.AssertionError: expected:<2> but was:<0>...at tw.idv.haocheng.play.CalculatorTest.one_plus_one_is_two(CalculatorTest.java:20)

FAIL

Page 31: Introduction to test_driven_development

Implement

Page 32: Introduction to test_driven_development

PASS

Page 33: Introduction to test_driven_development

Write the least code to make the test pass

Page 34: Introduction to test_driven_development

More Test

Page 35: Introduction to test_driven_development

java.lang.AssertionError: expected:<4> but was:<2>...at tw.idv.haocheng.play.CalculatorTest.two_plus_two_is_four(CalculatorTest.java:25)

FAIL

Page 36: Introduction to test_driven_development

Implement

Page 37: Introduction to test_driven_development

PASS

Page 38: Introduction to test_driven_development

Design

The add() method only accept positive numbers

Page 39: Introduction to test_driven_development

Test

Page 40: Introduction to test_driven_development

java.lang.AssertionError: IllegalArgumentException expectedat org.junit.Assert.fail(Assert.java:91)at tw.idv.haocheng.play.CalculatorTest.negative_numbers_will_throw_exception(CalculatorTest.java:32)

FAIL

Page 41: Introduction to test_driven_development

Implement

Page 42: Introduction to test_driven_development

PASS

Page 43: Introduction to test_driven_development

from http://www.oracle.com/

Page 44: Introduction to test_driven_development

Design

We need a addNewCustomer() method that will save a customer

record in Database

Page 45: Introduction to test_driven_development

Test

Page 46: Introduction to test_driven_development

Test

Page 47: Introduction to test_driven_development

java.lang.NullPointerExceptionat tw.idv.haocheng.CustomerDAOTest.addNewCustomer(CustomerDAOTest.java:24)

FAIL

Page 48: Introduction to test_driven_development

Implement

Page 49: Introduction to test_driven_development

PASS

Page 50: Introduction to test_driven_development

from https://www.google.com/accounts/NewAccount

Page 51: Introduction to test_driven_development

Design

We hava a page that user can apply for a new account

Page 52: Introduction to test_driven_development

Test

Page 53: Introduction to test_driven_development

Test

Page 54: Introduction to test_driven_development

wicket.markup.MarkupException: Unable to find component with id 'accountFormPanel' in [MarkupContainer ....at ngc.account.wicket.edit.CreateAccountPageTest.setUp(CreateAccountPageTest.java:49)

FAIL

Page 55: Introduction to test_driven_development

Implement

Page 56: Introduction to test_driven_development

PASS

Page 57: Introduction to test_driven_development

Unit Test Frameworks

● Java - JUnit● Python - PyUnit● PHP - PHPUnit● Ruby - Test:Unit● Javascript - Jasmine● .Net - NUnit

Page 58: Introduction to test_driven_development

There's No Silver Bullet

from http://www.penn-olson.com/2009/12/22/social-media-the-silver-bullet/

Page 59: Introduction to test_driven_development

Costs

from http://ora-pl-sql.blogspot.com/2011/05/database-costs.html

Page 60: Introduction to test_driven_development

It takes time...

from http://chunkeat626.blogspot.com/2010_11_01_archive.html

Page 61: Introduction to test_driven_development

Need to maintain tests

Page 62: Introduction to test_driven_development

from http://www.flickr.com/photos/ilike/2443295369/

TDD is not suitable for...

Page 63: Introduction to test_driven_development

from http://tw.gamelet.com/game.do?code=heroes

Page 64: Introduction to test_driven_development

from http://dilbert.com/

Page 65: Introduction to test_driven_development

But how do I start??

from http://zekjevets.blogspot.com/2010/02/alternative-racism.html

Page 66: Introduction to test_driven_development

from http://www.alexbolboaca.ro/wordpress/the-remote-pair-programming-tour

Page 67: Introduction to test_driven_development

Suggestions

● Prepare tools and environments

● Learn to write Unit test

● Start from easy ones

Page 68: Introduction to test_driven_development
Page 70: Introduction to test_driven_development

References - Test-Driven Development

● Test Driven Development Tutorial by Kirrily Robert● Engineer Notebook: An Extreme Programming Episode by

Robert C. Martin and Robert S. Koss● Technical Debt by Martin Fowler● InfoQ: Testing Misconceptions by Liam O'Connor● Unit Test Isolation● Erratic Test● Singletons are Evil ● RSpec 讓你愛上寫測試

Page 71: Introduction to test_driven_development