test driven development_continuous_integration

91
Test-Driven Development & Continuous Integration 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 。

Upload: haochenglee

Post on 07-May-2015

4.043 views

Category:

Technology


0 download

DESCRIPTION

This is the slides of my talk about test-driven development and continuous integration on 2011/06/11

TRANSCRIPT

Page 1: Test driven development_continuous_integration

Test-Driven Development&

Continuous Integration

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: Test driven development_continuous_integration

About me

● Hao-Cheng Lee● Java Engineer for 7+ years● Quality Engineer@Yahoo (till yesterday ;-))● Interested in Java, Scala, TDD, CI● email: [email protected]● twitter: https://twitter.com/#!/haocheng

Page 3: Test driven development_continuous_integration

Agenda

● What is TDD?

● Why use TDD?

● How to do TDD?

● What is CI?

● Why use CI?

● TDD + CI

Page 4: Test driven development_continuous_integration

TDD =

TFD +

Refactoring

Page 5: Test driven development_continuous_integration
Page 6: Test driven development_continuous_integration

Not about writing tests...

Page 7: Test driven development_continuous_integration

TDD is about writing better

code

Page 8: Test driven development_continuous_integration

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

Page 9: Test driven development_continuous_integration

Test Myths

Page 10: Test driven development_continuous_integration

I have no time for testing

Page 11: Test driven development_continuous_integration

Technical Debt

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

Page 12: Test driven development_continuous_integration

My code is BUG-FREE!

Page 13: Test driven development_continuous_integration

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

Page 14: Test driven development_continuous_integration

QA will do the testing

Page 15: Test driven development_continuous_integration

Black Box Testing

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

Page 16: Test driven development_continuous_integration

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

Page 17: Test driven development_continuous_integration

Faster

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

Page 18: Test driven development_continuous_integration

from Test Driven Development Tutorial by Kirrily Robert

Page 19: Test driven development_continuous_integration

Faster

● Shorter release cycle

● Automation saves time

● Find bugs earlier

Page 20: Test driven development_continuous_integration

Better

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

Page 21: Test driven development_continuous_integration

Better

● Greater code coverage

● The courage to refactor

● Prevent regression bugs

● Improve your design

Page 22: Test driven development_continuous_integration

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

Page 23: Test driven development_continuous_integration

Testing the Old Way

Page 24: Test driven development_continuous_integration

TDD

Page 25: Test driven development_continuous_integration

TDD

Page 26: Test driven development_continuous_integration

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 27: Test driven development_continuous_integration

Design

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

them together, then it will return the result.

Page 28: Test driven development_continuous_integration

Test

Page 29: Test driven development_continuous_integration

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

FAIL

Page 30: Test driven development_continuous_integration

Implement

Page 31: Test driven development_continuous_integration

PASS

Page 32: Test driven development_continuous_integration

Write the least code to make the test pass

Page 33: Test driven development_continuous_integration

More Test

Page 34: Test driven development_continuous_integration

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

FAIL

Page 35: Test driven development_continuous_integration

Implement

Page 36: Test driven development_continuous_integration

PASS

Page 37: Test driven development_continuous_integration

Design

The add() method only accept positive numbers

Page 38: Test driven development_continuous_integration

Test

Page 39: Test driven development_continuous_integration

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 40: Test driven development_continuous_integration

Implement

Page 41: Test driven development_continuous_integration

PASS

Page 42: Test driven development_continuous_integration

Unit Test Frameworks

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

Page 43: Test driven development_continuous_integration

Good Test

● One test per scenario● Test in isolation● Readability● Minimum Test Fixture● Repeatable

Page 44: Test driven development_continuous_integration

Bad Smell

● NO Assert/Meaningless Assert● High maintenance cost● Interacting Tests● Require manual debugging ● Evil Singleton

Page 45: Test driven development_continuous_integration

When is enough enough?

● One Test per class● Testing the feature● Find bugs, add tests● Skip Getter/Setter if generated● Skip Private methods● Code coverage

Page 46: Test driven development_continuous_integration

There's No Silver Bullet

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

Page 47: Test driven development_continuous_integration

It takes time...

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

Page 48: Test driven development_continuous_integration

Need to maintain tests

Page 49: Test driven development_continuous_integration

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

TDD is not suitable for...

Page 50: Test driven development_continuous_integration

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

Page 51: Test driven development_continuous_integration

from http://dilbert.com/

Page 52: Test driven development_continuous_integration

Continuous Integration

Page 53: Test driven development_continuous_integration

Continuous Integration is a software development practice where members of a team integrate their work frequently, usually each person integrates at least daily – leading to multiple integrations per day. -- Martin Fowler

Page 54: Test driven development_continuous_integration

Why CI?

●Rapid Feedback

●Reduced Risk

●Collective Ownership

●Continuous Deployment

●Offload from people

Page 55: Test driven development_continuous_integration

Why TDD + CI?

Page 56: Test driven development_continuous_integration

Effective tests must be automated

from http://www.laurentbrouat.com/why-you-should-stop-sending-auto-dms/automated/

Page 57: Test driven development_continuous_integration

Write once, run often

●Write tests once●Run frequently●No human input●Common output

Page 58: Test driven development_continuous_integration

Best Practices of CI

●Single Source Repository

●Commit often

●Make Your Build Self-Testing

●Automate the Build

●Build fast

Page 59: Test driven development_continuous_integration

Extensible continuous integration server

Page 60: Test driven development_continuous_integration

What is Jenkins?

●Open-source CI server

●Easy to install and use

●Extensibility

Page 61: Test driven development_continuous_integration

Why Jenkins?

●GUI to manage

●Strong community and eco-system

●Distributed builds

●Open Source and Free!

Page 62: Test driven development_continuous_integration

mailing list subscription is increasing

Page 63: Test driven development_continuous_integration

GitHub members is also increasing

Page 64: Test driven development_continuous_integration

Basic Features

●Notice a change

●Check out source code

●Execute builds/tests

●Record and publish results

●Notify developers

Page 65: Test driven development_continuous_integration

CI Overview

from Continuous integration with Hudson

Page 66: Test driven development_continuous_integration

Notice a change

●Build Periodically

●Depend on other projects

●Poll SCM○Subversion Push vs. Pull

Page 67: Test driven development_continuous_integration

Check out source code

●Subversion

●CVS

●Git

●Mercurial

●Perforce

Page 68: Test driven development_continuous_integration

Execute builds/tests

●Java○ Ant, Maven, Gradle

●.Net○ MSBuild, PowerShell

●Shell Script○ Python, Ruby, Groovy

Page 69: Test driven development_continuous_integration

Record and publish results

●JUnit●TestNG●Findbugs●Cobertura●Checkstyle●PMD

Page 70: Test driven development_continuous_integration

Job Status

Job State: Job Stability:

Page 71: Test driven development_continuous_integration

Findbugs Integration

Page 72: Test driven development_continuous_integration

Cobertura Integration

Page 73: Test driven development_continuous_integration

Project Relationship

Page 75: Test driven development_continuous_integration
Page 76: Test driven development_continuous_integration

Twitter

Page 77: Test driven development_continuous_integration

Jenkins on Eclipse

Update Site: http://code.google.com/p/hudson-eclipse/

Page 78: Test driven development_continuous_integration

Jenkins on Android

● Android Market● Jenkins Wiki

Page 79: Test driven development_continuous_integration

eXtreme Feedback Panel plugin

Page 80: Test driven development_continuous_integration

Jenkins Sound pluginhttp://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Sounds+plugin

Page 81: Test driven development_continuous_integration

Installation&Upgrade

● Download Tomcat 7

● Download latest jenkins.war

● Put jenkins.war under webapps

● Start Tomcat

Page 82: Test driven development_continuous_integration

Create a Job

Page 83: Test driven development_continuous_integration

Configure a Job

Page 84: Test driven development_continuous_integration

Configure Jenkins

Page 85: Test driven development_continuous_integration

Manage Plugins

Page 87: Test driven development_continuous_integration
Page 89: Test driven development_continuous_integration

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 90: Test driven development_continuous_integration

References - Continuous Integration

● Jenkins: http://jenkins-ci.org/● Mailing List: http://groups.google.com/group/jenkinsci-

users ● Wiki: http://wiki.jenkins-ci.org/ ● Follow @jenkinsci on Twitter● Continous Integration by Martin Fowler● Continuous Integration with Hudson - the book● Continuous Integration with Hudson on JavaWorld

Page 91: Test driven development_continuous_integration