Improve your development skills with Test Driven Development

1,891 views
1,726 views

Published on

A talk at the Graduate Developer community on test driven development. Helping students and graduates understand the point of a test first approach, demonstrate the tools and show how easy it is to start.

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,891
On SlideShare
0
From Embeds
0
Number of Embeds
412
Actions
Shares
0
Downloads
10
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • If we think about designng a museum, what does it need to do. Well we want to hold a lot of interesting pieces and we want lots of people to come along . So it needs to be a big spacious building. We need people to enter the building so we need people sized doors . We want to charge people, so we need to filter people through a payment channel. We need walls to hang things on and to keep out the cold. The building needs to be secure as there will be valuable itens that some people may want to remove, so we need some sort of security. We want to keep the items in good condition, so the internal atmosphere of the museum needs to be regularted, so we need some sort of plumbing
  • Improve your development skills with Test Driven Development

    1. 1. Open Source Jumpstart: Improve your development Skills©LeanAgileMachineCreative Commons License
    2. 2. Graduate Developer Community Meet with Companies www.grad-dc.co.uk looking for interns Talk to experienced blog.grad-dc.co.uk developers & testers Ask questions about what you learnt tonight©LeanAgileMachineCreative Commons License Socialse
    3. 3. Goals of OS Jumpstart ● What tools are out there ● What should you know ● Show how Unit Testing tools support the agile process©LeanAgileMachineCreative Commons License
    4. 4. Goals of this session ● Understand the value of TDD ● Give you confidence to start learning TDD ● Tools to use to start learning ● I cant teach you TDD in one evening, sorry! ● TDD is as big as learning something like OO ● A bit of practical experience ● Pair up and try it out ● Deliberate practice©LeanAgileMachineCreative Commons License
    5. 5. Have you tried ”Test First” ? Raise your hand if you already tried TDD and ”test first” development ?©LeanAgileMachineCreative Commons License
    6. 6. Where does TDD fit©LeanAgileMachineCreative Commons License
    7. 7. So what is TDD ??©LeanAgileMachineCreative Commons License
    8. 8. A design activity ● What is the challenge (the requirements) ● What are the concepts we are dealing with ● What is valuable ● What behaviour do I need to understand ● How do I know I have met the challenge ● How do I know I can change©LeanAgileMachineCreative Commons License
    9. 9. Understanding the problem ● Behaviour ● Concepts ● Stakeholders ● Values ● Priorities©LeanAgileMachineCreative Commons License
    10. 10. The TDD cycle ● Write a failing test so we know it tests something ● Write code to make the test pass ● Refactor code to be as simple as possible©LeanAgileMachineCreative Commons License
    11. 11. Continuous / Fast Feedback● Are we there yet ?● Your own ”mini-me” tester as a guide ● Only scratching the surface of testing though©LeanAgileMachineCreative Commons License
    12. 12. What tools are involved Jenkins CI©LeanAgileMachineCreative Commons License
    13. 13. Anatomy of a test©LeanAgileMachineCreative Commons License
    14. 14. Make the test compile©LeanAgileMachineCreative Commons License
    15. 15. Write a class©LeanAgileMachineCreative Commons License
    16. 16. Common assertions ● assertEquals() ● Two objects of the same type are equal in value ● assertNotNull() ● The object does not have a null value ● assertTrue() ● The statement results is a true evaluation ● assertSame() ● Two objects are the same http://junit.sourceforge.net/javadoc/org/junit/Assert.html©LeanAgileMachineCreative Commons License
    17. 17. JUnit Annotations● @Test ● @Ignore ● Defines a method as a test ● Dont run this● @Test(expected=exception.class) test – use ● Fails if a specific exception is no sparingly thrown● @Test(timeout = 1000) ● Fails if test takes too long (milliseconds) http://junit.sourceforge.net/javadoc/org/junit/Test.html ©LeanAgileMachine Creative Commons License
    18. 18. Test ”smells” ● Testing code rather than behaviour ● Multiple assertions ● Too specific per method ● Changing tests every ● Bloated tests time you refactor ● Harder to refactor ● Addressing to many scenarios ● Harder to understand and maintain©LeanAgileMachineCreative Commons License
    19. 19. A bad test example @Test public void equalsCheck() { System.out.println("* VectorsJUnit4Test: equalsCheck()"); assertTrue(Vectors.equal(new int[] {}, new int[] {})); assertTrue(Vectors.equal(new int[] {0}, new int[] {0})); assertTrue(Vectors.equal(new int[] {0, 0}, new int[] {0, 0})); assertTrue(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0, 0})); assertTrue(Vectors.equal(new int[] {5, 6, 7}, new int[] {5, 6, 7})); assertFalse(Vectors.equal(new int[] {}, new int[] {0})); assertFalse(Vectors.equal(new int[] {0}, new int[] {0, 0})); assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0, 0, 0})); assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0})); assertFalse(Vectors.equal(new int[] {0, 0}, new int[] {0})); assertFalse(Vectors.equal(new int[] {0}, new int[] {})); assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 0, 1})); assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {0, 1, 0})); assertFalse(Vectors.equal(new int[] {0, 0, 0}, new int[] {1, 0, 0})); assertFalse(Vectors.equal(new int[] {0, 0, 1}, new int[] {0, 0, 3})); }©LeanAgileMachineCreative Commons License
    20. 20. Quick Netbeans / JUnit demo - create a new project - create your first test - run the JUnit testrunner - write code to fix the test - run the JUnit testrunner - write another test...©LeanAgileMachineCreative Commons License
    21. 21. Your turn - pair up - fire up netbeans - start a new Java Application project - or check out my project - read the basic requirements - write a test...©LeanAgileMachineCreative Commons License
    22. 22. Deliberate practice● Goal is to practice TDD ● Become comfortable with the tools ● Gain experience in writing tests ● Improve confidence in TDD● The resulting code algorithms are not important ● I dont care how fast your calculator calculates ● Youre here to learn not to produce©LeanAgileMachineCreative Commons License
    23. 23. Hamcrest asserts ● Hamcrest is a pattern matching library ● Can help make you test logic more human readable AssertThat() - instead of JUnit asserts assertThat(myCalc.addTwoNumbers(6, 7), is(6 + 7)); assertThat(myCalc.addTwoNumbers(6, 7), is(not(6 + 9)));©LeanAgileMachineCreative Commons License
    24. 24. Build Tools ● Build Automation ● Compile, test and deploy code from your repository Ant.apache.org ant.apache.org/ivy Maven.apache.org Comparison of tools: http://ant.apache.org/ivy/ m2comparison.html©LeanAgileMachineCreative Commons License
    25. 25. Integrated Development Environments● Manage large / multiple projects ● Debugger ● Autocompletion (code api completion) ● Refactoring .org● Build management Ant / Maven – compile, test, deploy .org ●● Integration ● Application servers, databases, unit testing, Scm, bug tracker, CI server, IntelliJ IDEA jetbrains.org©LeanAgileMachineCreative Commons License
    26. 26. Keep the build bunny happy● Tests help prevent build errors● You can run your tests before you anger the build bunny©LeanAgileMachineCreative Commons License
    27. 27. Collaborative workspace ● Can show progress of your tests Confluence ● Show you which tests are failing ● Gives everyone a sence of progress©LeanAgileMachineCreative Commons License
    28. 28. Summary ● TDD is a design activity ● Gives you time to use your brain before coding ● This session was just enough to get you going ● Try doing some coding kata ● Try a coding dojo or code retreat ● LJC / GDC organising these soon ! ● Monthly Python, Clojure, Scala dojos aready running©LeanAgileMachineCreative Commons License
    29. 29. We want your feedback©LeanAgileMachineCreative Commons License
    30. 30. Graduate Developer Communitywww.grad-dc.co.uk Meet with Companiesblog.grad-dc.co.uk looking for interns Talk to experienced developers & testers Ask questions about what you learnt tonight Socialse©LeanAgileMachineCreative Commons License
    31. 31. TDD / BDD Workshop ● Half day workshop at SkillsMatter 17 th May ● Part of London Tester Gathering days http://bit.ly/LTGDays2011©LeanAgileMachineCreative Commons License
    32. 32. Thank youUseful linksJohn@jr0cket.comJohn.Jr0cket.co.ukToolingup.Jr0cket.co.ukUbuntu.Jr0cket.co.uk Growing OO Software,www.grad-dc.co.uk guided by testsblog.grad-dc.co.uk ©LeanAgileMachine Creative Commons License

    ×