1 / 36

Test-Driven Development and Code Coverage

Test-Driven Development and Code Coverage. An Evening of Robert Burns Poetry. Readings by Craig Murphy. Session Overview. “The simplest thing that could possibly work” This session will provide an explanation and demonstration of: How test-driven development (TDD) works

ilori
Download Presentation

Test-Driven Development and Code Coverage

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Test-Driven Development and Code Coverage

  2. An Evening of Robert Burns Poetry Readings by Craig Murphy

  3. Session Overview • “The simplest thing that could possibly work” • This session will provide an explanation and demonstration of: • How test-driven development (TDD) works • How code-coverage works • How to merge TDD and code coverage • A selection of tools that can help us work with TDD and code coverage • Code examples will be in C# using Visual Studio 2005 • Roman numeral conversion and a simple calculator • Not “text-book” examples – they tend to be “hard going” • I have a lot to cover, so this should be a fast-paced session • I may leave you to browse the Resources slides in your own time • I’m enthusiastic! Please tell me if I am going too fast! Test Driven Development and Code Coverage

  4. Housekeeping Mobile ‘phones Fire Exits Toilets Automating the Build Process Using FinalBuilder 4

  5. About Me XML XSLT XQuery XML Schema SOAP WML IntraWeb Web Services C# InterOp with Delphi RUP UML TDD in C#, VB.net and Delphi 8 Scrum • 50+ presentations delivered: • NRW06, NRW07 • DeveloperDeveloperDeveloper (UK Community Events) • Scottish Developers • Agile Scotland • British Computer Society (BCS) • UK Borland User Group (DDG) • Visual Basic User Group (VBUG) • VBUG .net Winter 2001 conference • XML One 2001 • 60+ articles published: • The Delphi Magazine • developers’ magazine (Dotnet Developers’ Group - DDG) • ASPToday.com (now Wiley, previously Wrox) • ASP.NET Pro, International Developer • CSharpCorner, DeveloperFusion Automating the Build Process Using FinalBuilder

  6. Agenda • Motivation • The Tools • Test-Driven Development • Code Coverage • Summary • Resources Test Driven Development and Code Coverage

  7. Motivation • There are times when we are too focused on application development • New/useful tools techniques are passed by • 60-90 minute sessions like these, personally, help me save time by: • Identifying new/useful tools and techniques • Demonstrating new/useful tools and techniques • Test-Driven Development (TDD) sits nicely in the eXtreme Programming (XP) “way of doing things” • TDD can be used without practicing XP • Search msdn.microsoft.com for TDD… • approximately 155,000 results Test Driven Development and Code Coverage

  8. The Tools • NUnit 2.4.6 • Testing framework for .NET • Graphical front-end (clear “black/white” pass/fail indicator) • NCover 1.5.8 • “Do you know how much of your code is being tested” • Command-line • Creates an XML output file, XSL transformation for viewing • NCoverExplorer 1.4.0.7 • Graphical front-end for NCover • Windows application that uses NCover’s XML output file • TestDriven.NET 2.11.2177 • NUnit, NCover, NCoverage integration inside the Visual Studio IDE • Right-click context-sensitive menu • Free “liberal” personal edition; commercial Profession and Enterprise editions Test Driven Development and Code Coverage

  9. Agenda • Motivation • The Tools • Test-Driven Development • Code Coverage • Summary • Resources Test Driven Development and Code Coverage

  10. TDD – Agenda • What is TDD? • TDD Stages • TDD in C# • Demonstration • Why TDD? Test Driven Development and Code Coverage

  11. What is TDD? • “Before you write code, think about what it will do. Write a test that will use the methods you haven’t even written yet.” • Extreme Programming Applied: Playing To Win • Ken Auer, Roy Miller • “The Purple Book” • A test is not something you “do”, it is something you “write” and run once, twice, three times, etc. • It is a piece of code • Testing is therefore “automated” • Repeatedly executed, even after small changes • “TDD is risk averse programming, investing work in the near term to avoid failures later on” • Munjal Budhabhatti, The Architecture Journal 14 Test Driven Development and Code Coverage

  12. What is TDD? • TDD is a technique whereby you write your test cases before you write any implementation code • Forces developers to think in terms of implementer and user • Tests drive or dictate the code that is developed • “Do the simplest thing that could possibly work” • Developers have less choice in what they write • An indication of “intent” • Tests provide a specification of “what” a piece of code actually does – it goes some way to defining an interface • Some might argue that “tests are part of the documentation” • Could your customers/clients write tests? Test Driven Development and Code Coverage

  13. TDD - Agenda • What is TDD? • TDD Stages • TDD in C# • Demonstration • Why TDD? Test Driven Development and Code Coverage

  14. TDD Stages • In Extreme Programming Explored (The Green Book), Bill Wake describes the test / code cycle: • Write a single test • Compile it. It shouldn’t compile because you’ve not written the implementation code • Implement just enough code to get the test to compile • Run the test and see it fail • Implement just enough code to get the test to pass • Run the test and see it pass • Refactor for clarity and “once and only once” • Repeat Test Driven Development and Code Coverage

  15. TDD Stages Write a test Refactor code(and test) Compile Run test, watch it pass Fix compile errors Write code Run test,watch it fail Test Driven Development and Code Coverage

  16. Smells • During TDD, particularly the refactoring step, consider these smells: • Duplication • Once And Once Only (OAOO) • Setup / TearDown (NUnit attributes) • Some duplicated code reveals itself as common ‘initialisation’ code • And / Or as ‘cleanup’ code Test Driven Development and Code Coverage

  17. TDD in C#, Visual Basic.net, Delphi 2005/6 • NUnit, csUnit, MbUnit • Are three xUnit implementations for .net languages • “MbUnit has been referred to as “unit testing on crack” [Hanselman] • xUnit is a colloquial umbrella term • Platform-specific and language-specific implementations • e.g. sUnit, JUnit, XMLUnit • Richard Fennell spoke about TSQLUnit at SQLBits, October 2007 • NUnit, csUnit and MbUnit • Provides an attribute-based testing framework • Attributes “instrument” code, adding meaning and intent • Makes use of Assert statements • Tests are written using Assertions • Actual and Expected results need to be known… Test Driven Development and Code Coverage

  18. TDD in C# using NUnit Assert.AreEquals( expected, actual, msg ); [C#] [TestFixture] public class TestRoman1 { [Test] public void Roman1() { ClsRoman rc = new ClsRoman(); Assert.AreEqual("I",rc.toRoman(1),"1 fails to convert"); } } Test Driven Development and Code Coverage

  19. Test-driven development using C# and NUnit demo Test Driven Development and Code Coverage

  20. Test Complexity • Do the simplest thing • Dave Astels: “Strive for simplicity” • Write a test that fails (red) • Make the test pass (green) • Refactor implementation code (change/improve the internal design) • Use the compiler – let it tell you about errors and omissions • Today, compiler time is cheap • One assertion (Assert) per test • Has been the subject of furious debate on Yahoo’s TDD group Test Driven Development and Code Coverage

  21. Small Steps Are Good • TDD promotes “small steps”, and lots of them • Small steps: the shortest distance between two points • Your destination is closer… • Red route • incurs “scrap and re-work” • Takes longer • May not cleanly reach its destination B A Test Driven Development and Code Coverage

  22. Test Case Generation • Automate using tools • e.g. Kellerman Software’s NUnit Test Generator • http://www.kellermansoftware.com • Works with Visual Studio 2002 upwards…

  23. Agenda • Motivation • The Tools • Test-Driven Development • Code Coverage • Summary • Resources Test Driven Development and Code Coverage

  24. Code Coverage – Agenda • What is Code Coverage? • Demonstration • Code Coverage • Integrating Unit Tests and Code Coverage • Why Code Coverage? Test Driven Development and Code Coverage

  25. What is Code Coverage? • Code Coverage is an approach or technique that sees you executing [all] the statements and paths through your code-base • In Code Complete 2, McConnell refers to code coverage and logic coverage as one and the same • e.g. Ensure that the all the paths through code are evaluated • e.g. if (…) … else … conditions are evaluated • Without tools/automation, code coverage is a labour-intensive activity • Manually updating code coverage reports was a chore • It is important to know/define what we lessons we can learn from code coverage • e.g. code coverage cannot help us with multi-parameter APIs with large numbers of input parameters and large numbers of return values… • Unless there are implicit tests in place Test Driven Development and Code Coverage

  26. Code Coverage using C#, NUnit, NCover and NCoverage demo Test Driven Development and Code Coverage

  27. Agenda • Motivation • The Tools • Test-Driven Development • Code Coverage • Summary • Resources Test Driven Development and Code Coverage

  28. Test-Driven DevelopmentSummary • TDD may appear to require more development time, however, in reality: • Developer-level feature creep is avoided, no “over-implementation” • Quality of client-facing features is exceptionally high • TDD does not replace traditional testing • It defines a proven way that ensures effective unit testing • Tests are working examples of how to invoke a piece of code • Essentially provides a working specification for the code • Ideally, no code should go into production unless it has associated tests • Catch bugs before they are shipped to your customer • Often referred to as “No code without tests” • Tests determine, or dictate, the code • The programmers should write the tests • The programmers can’t wait for somebody else to write tests • It’s all about early problem identification, early find, early fix, reduced cost Test Driven Development and Code Coverage

  29. Code Coverage Summary • Identifies code that your tests missed • Thus, you should find bugs you may not otherwise have found, code coverage “gives you clues” • Identifies code that does not have a test case exercising it • TDD helps me with the low-level functionality, code coverage helps me test Windows-based user interfaces • Code coverage can be a useful metric, but it must be used with care • Determine whether high code coverage is a good thing • Simply “touching” every line of code might not be suitable • Do not be sucked into “writing quick tests” just to improve coverage • Brian Marick on code coverage tools: “…they are only helpful…to enhance thought, not replace it” Test Driven Development and Code Coverage

  30. Resources (Books) test-driven development: A Practical Guide Dave Astels Prentice-Hall/Pearson Education, 2003 ISBN 0-13-101649-0 Review available on my web-site ______________________________________ Test-Driven Development: By Example Kent Beck Addison-Wesley, 2003 ISBN 0-321-14653-0 Test Driven Development and Code Coverage

  31. Resources (Books) Refactoring: Improving the Design of Existing Code Martin Fowler Addison-Wesley, 1999 ISBN 0-201-48567-2 __________________________________ Test-Driven Development in Microsoft.NET James W. Newkirk; Alexei A. Vorontsov Microsoft Press, 2004 ISBN 0-7356-1948-4 xUnit.net & xUnit Test Pattern Test Driven Development and Code Coverage

  32. Test-Driven DevelopmentResources (web-sites) • NUnit: http://www.nunit.org • Charlie Poole’s blog “It’s the tests” • http://nunit.com/blogs/ • http://www.testdriven.net • Jamie Cansdale’s Visual Studio integration • Personal (free), Professional ($), Enterprise ($) • CSUnit: http://www.csunit.org • Manfred Lange’s .net testing framework • http://groups.yahoo.com/group/csunit • MbUnit: http://www.mbunit.com • http://www.nxtgenug.net/Article.aspx?ArticleID=107 Test Driven Development and Code Coverage

  33. Test-Driven DevelopmentResources (web-sites) • Lutz Roeder’s .NET Reflector: • http://www.aisto.com/roeder/dotnet • xUnit implementations: • http://www.xprogramming.com/software.htm • http://www.junit.org • http://www.vbunit.org • http://www.testdriven.com (Brian Marick’s site) • Clarke Ching on Roman Numeral conversion in Microsoft Excel: • http://www.clarkeching.com/2006/04/test_driven_dev.html • Ideal if you need to “sell TDD” to your manager! • Improving Application Quality Using Test-Driven Developmenthttp://www.methodsandtools.com/archive/archive.php?id=20 Test Driven Development and Code Coverage

  34. Code CoverageResources (web-sites) • testdrivendevelopment group (Yahoo): • http://groups.yahoo.com/group/testdrivendevelopment • Ron Jeffries, Dave Astels, Kent Beck, etc. are regular contributors • NCover: http://www.ncover.org • Peter Waldschmidt’s command-line code coverage for .NET • NCoverExplorer: http://www.kiwidude.com/blog/2006/01/ncoverexplorer-debut.html • Grant Drake’s front-end for NCover • NCover Browser http://www.sliver.com/dotnet/NCoverBrowser/ • Jeff Key’s early front-end for NCover • No source code available… • http://www.testdriven.com (Brian Marick’s site) • http://www.testing.com/writings/coverage.pdf • http://en.wikipedia.org/wiki/Code_coverage Test Driven Development and Code Coverage

  35. Contact Information Craig Murphy http://www.twitter.com/CAMURPHY Updated slides, notes and source code: http://www.CraigMurphy.com http://www.CraigMurphy.com/blog

  36. Questions? RED GREEN REFACTOR Test Driven Development and Code Coverage

More Related