Your SlideShare is downloading. ×
Test-Driven Development for Developers: Plain and Simple
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Test-Driven Development for Developers: Plain and Simple

200
views

Published on

Test-driven development (TDD) is not an easy discipline to establish. However, it provides considerable return on investment for the effort. Rob Myers describes the costs of TDD (the introduction of …

Test-driven development (TDD) is not an easy discipline to establish. However, it provides considerable return on investment for the effort. Rob Myers describes the costs of TDD (the introduction of test-maintenance overhead) and its benefits (greatly improved quality, productivity, and throughput of real value)—but only when the TDD practices are given time to ripen. Rob shares a simple three-step process for establishing the personal and professional discipline required to successfully implement TDD and takes you through a simple yet realistic demo to reveal three core TDD techniques—Triangulation, Fake It, and Obvious Implementation. Rob uses this demo to show how new objects can reveal themselves to developers via "obvious necessity" thus destroying the myth that all TDD design must arise from either specification or refactoring. In this demo, Rob uses Java and JUnit but the principles and techniques described apply to any object-oriented programming efforts in any programming language.

Published in: Technology

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
200
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1.   AT7 Concurrent Session  11/14/2013 2:15 PM        "Test-Driven Development for Developers: Plain and Simple"       Presented by: Rob Myers Agile Institute               Brought to you by:        340 Corporate Way, Suite 300, Orange Park, FL 32073  888‐268‐8770 ∙ 904‐278‐0524 ∙ sqeinfo@sqe.com ∙ www.sqe.com
  • 2. Rob Myers Agile Institute Rob Myers is founder of the Agile Institute and a founding member of the Agile Cooperative. With twenty-seven years of professional experience on software development teams, Rob has consulted for leading companies in aerospace, government, medical, software, and financial sectors. He has been training and coaching organizations in Scrum and Extreme Programming (XP) management and development practices since 1999. Rob’s courses—including Essential Test-Driven Development and Essential Agile Principles and Practices—are a blend of enjoyable, interactive, hands-on labs plus practical dialog toward preserving sanity in the workplace. Rob performs short- and long-term coaching to encourage, solidify, and improve the team's agile practices.
  • 3. 9/10/13   Café TDD for Developers: Plain & Simple Rob Myers for Agile Development Practices East 14 November 2013 10 September 2013 © Agile Institute 2008-2013 1 10 September 2013 © Agile Institute 2008-2013 2 1  
  • 4. 9/10/13   Unit testing is soooo DEPRESSING 10 September 2013 © Agile Institute 2008-2013 3 TDD is fun, and provides much more than just unit-tests! 10 September 2013 © Agile Institute 2008-2013 4 2  
  • 5. 9/10/13   “The results of the case studies indicate that the pre-release defect density of the four products decreased between 40% and 90% relative to similar projects that did not use the TDD practice. Subjectively, the teams experienced a 15–35% increase in initial development time after adopting TDD.” http://research.microsoft.com/en-us/projects/esm/nagappan_tdd.pdf, Nagappan et al, © Springer Science + Business Media, LLC 2008 10 September 2013 © Agile Institute 2008-2013 5 = 10 September 2013 © Agile Institute 2008-2013 6 3  
  • 6. 9/10/13   discipline 10 September 2013 © Agile Institute 2008-2013 7 10 September 2013 © Agile Institute 2008-2013 8 4  
  • 7. 9/10/13   TDD Demo 10 September 2013 © Agile Institute 2008-2013 9 no magic import junit.framework.Assert; import org.junit.Test; public class SetTests { @Test public void setStartsOutEmpty() { Set mySet = new Set(); Assert.assertEquals(0, mySet.size()); } } 10 September 2013 © Agile Institute 2008-2013 10 5  
  • 8. 9/10/13   requirements & conventions import junit.framework.Assert; import org.junit.Test; public class SetTests { @Test public void setStartsOutEmpty() { Set mySet = new Set(); Assert.assertEquals(0, mySet.size()); } } Expected 10 September 2013 Actual © Agile Institute 2008-2013 11 start simple To Do q Empty set. q Adding an item. q Adding two different items. q Adding the same item twice. 10 September 2013 © Agile Institute 2008-2013 12 6  
  • 9. 9/10/13   specification, and interface import junit.framework.Assert; import org.junit.Test; public class SetTests { @Test public void setStartsOutEmpty() { Set mySet = new Set(); Assert.assertEquals(0, mySet.size()); } } Will not compile. L 10 September 2013 © Agile Institute 2008-2013 13 “Thank you, but…” package supersets; public class Set { public int size() { throw new RuntimeException("D'oh! Not yet implemented!"); } } 10 September 2013 © Agile Institute 2008-2013 14 7  
  • 10. 9/10/13   “…I want to see it fail successfully” public class Set { public int size() { return -1; } } 10 September 2013 © Agile Institute 2008-2013 15 technique: “Fake It” package supersets; public class Set { public int size() { return 0; } } 10 September 2013 © Agile Institute 2008-2013 16 8  
  • 11. 9/10/13   a. refactor away duplication @Test public void setStartsOutEmpty() { Set mySet = new Set(); Assert.assertEquals(0, mySet.size()); } public class Set { public int size() { return 0; } } 10 September 2013 © Agile Institute 2008-2013 17 b. triangulate 10 September 2013 © Agile Institute 2008-2013 18 9  
  • 12. 9/10/13   technique: “Triangulation” @Test public void addingItemIncreasesCount() { Set mySet = new Set(); mySet.add("One item"); Assert.assertEquals(1, mySet.size()); } @Test public void setStartsOutEmpty() { Set mySet = new Set(); Assert.assertEquals(0, mySet.size()); } 10 September 2013 © Agile Institute 2008-2013 19 technique: “Obvious Implementation” import java.util.ArrayList; import java.util.List; public class Set { private List innerList = new ArrayList(); public int size() { return innerList.size(); } public void add(Object element) { innerList.add(element); } } 10 September 2013 © Agile Institute 2008-2013 20 10  
  • 13. 9/10/13   maintain (refactor) the tests Set mySet; @Before public void initializeStuffCommonToThisTestClass() { mySet = new Set(); } @Test public void addingItemIncreasesCount() { mySet.add("One item"); Assert.assertEquals(1, mySet.size()); } @Test public void setStartsOutEmpty() { Assert.assertEquals(0, mySet.size()); } 10 September 2013 © Agile Institute 2008-2013 21 now for something interesting @Test public void addingEquivalentItemTwiceAddsItOnlyOnce() { String theString = "Equivalent string"; mySet.add(theString); mySet.add(new String(theString)); Assert.assertEquals(1, mySet.size()); } 10 September 2013 © Agile Institute 2008-2013 22 11  
  • 14. 9/10/13   the new behavior public class Set { private List innerList = new ArrayList(); public int size() { return innerList.size(); } public void add(Object element) { if (!innerList.contains(element)) innerList.add(element); } } 10 September 2013 © Agile Institute 2008-2013 23 next? To Do q Empty set. q Adding an item. q Adding two different items. q Adding the same item twice. 10 September 2013 © Agile Institute 2008-2013 24 12  
  • 15. 9/10/13   1.  Write one unit test. steps 2.  Build or add to the object under test until everything compiles. 3.  Red: Watch the test fail! 4.  Green: Make all the tests pass by changing the object under test. 5.  Clean: Refactor mercilessly! 6.  Repeat. 10 September 2013 © Agile Institute 2008-2013 25 Rob.Myers@agileInstitute.com http://PowersOfTwo.agileInstitute.com/ @agilecoach 10 September 2013 © Agile Institute 2008-2013 26 13