Post on 31-Dec-2015
© Copyright 2005, thycotic.
Test Driven DevelopmentTest Driven Development
Jonathan Cogley
Maryland Cold Fusion User's Group 10/11/2005
© Copyright 2005, thycotic.
Jonathan CogleyJonathan Cogley
• CEO and Founder of Thycotic– Focus on .NET, ASP.NET, Microsoft SQL Server
• Microsoft MVP for C#• Editor and columnist for AspAlliance• ASPInsider• Speaker at UGs and Conferences• TDD Evangelist
© Copyright 2005, thycotic.
DisclaimerDisclaimer
• I have only been coding in ColdFusion since 8:30pm last night when I installed it for the first time.
• But I am here to tell you about TDD not CF …
© Copyright 2005, thycotic.
What can you expect?What can you expect?
• “Clean code that works”- Ron Jeffries
• Radically change your software development style
• Relieve stress/fear• Methodical approach• Higher quality software
© Copyright 2005, thycotic.
AgendaAgenda
• Review past development– Will it work for today’s applications?
• TDD – Brief Overview• HANDS ON
– Real World TDD
• Review the tough questions• Q & A
© Copyright 2005, thycotic.
Typical CF Development?Typical CF Development?
• Build a script class or CFC component
• Create a test page• Publish the component• Delete the test page
© Copyright 2005, thycotic.
DisadvantagesDisadvantages
• Not a methodical approach• Test page reinvents the wheel and
provides a poor test harness• Test page is usually deleted• Only tests component in isolation• Not automated
© Copyright 2005, thycotic.
Is this process manageable Is this process manageable for today’s applications?for today’s applications?
• NO!• Not up to the job
- increased number of classes- increased sharing of classes- more developers working on same code
• There must be a better way?
© Copyright 2005, thycotic.
Extreme Programming Extreme Programming BackgroundBackground
• Kent Beck,Ward Cunningham,Ron Jeffries
• TDD is an integral part of Extreme Programming
• Why is it extreme?• XP is a huge topic!
– Software Development Methodology
© Copyright 2005, thycotic.
Test Driven DevelopmentTest Driven Development
• Test– Not really a test in any conventional
fashion
• Driven– Integral to the process
• Development– Remnant from old phase approach
© Copyright 2005, thycotic.
How do XP and TDD relate?How do XP and TDD relate?
• TDD is part of XP
• Complementing parts of XP– Collective Code Ownership– Coding Standards – Automated Tools– Pair Programming
© Copyright 2005, thycotic.
What are the basic rules?What are the basic rules?
• Complete set of tests• Can’t write code without a test
failing• Write the tests first• Tests reflect the features required• Do the simplest thing to pass• Remove duplication / Refactor
© Copyright 2005, thycotic.
CFUnitCFUnit
• http://cfunit.sourceforge.net/• Open source and free• Tag based unit testing framework• Allows for test fixtures and suites• Browser dependent – workaround?• Has standard xUnit features
© Copyright 2005, thycotic.
© Copyright 2005, thycotic.
The TDD CycleThe TDD Cycle
Add a testAdd a test
Make it compileMake it compile
Watch test failWatch test fail
Write a little codeWrite a little code
Watch test passWatch test pass RefactorRefactor Watch test passWatch test pass
© Copyright 2005, thycotic.
TDD DEMOTDD DEMO
• XP and TDD is best introduced by example
“pair with the new guy or gal”
© Copyright 2005, thycotic.
Our TODO ListOur TODO List
• Try some basic sorting using a CFC– Audience participation essential!
© Copyright 2005, thycotic.
TDD TechniquesTDD Techniques
• Fake it
• Obvious Implementation
• Triangulation
© Copyright 2005, thycotic.
Ensuring Quality TestsEnsuring Quality Tests
• Test according to requirements
• Statement coverage• Defect insertion
MetricsMetrics
© Copyright 2005, thycotic.
And in the .NET Camp … NUnitAnd in the .NET Camp … NUnit
• Automated testing tool• Originally ported from JUnit• Rewritten to take advantage
of .NET features in NUnit 2.0• Open source• http://nunit.sourceforge.net
© Copyright 2005, thycotic.
NUnit in a NutshellNUnit in a Nutshell
• NUnit.Framework namespace• [TestFixture]• [SetUp] [TearDown]• [Test]• [ExpectedException]• Assert class
– IsTrue– AreEqual
© Copyright 2005, thycotic.
Other NUnit ExtensionsOther NUnit Extensions
• NUnitAddIn– Add-in for VS.NET
• NUnit task for the NAnt build tool• NUnitAsp
© Copyright 2005, thycotic.
GUI TestingGUI Testing
• Keep logic in your BL • NUnitAsp
– Web Extension for NUnit– Create custom testers for your own
controls– Great possibilities for inheriting
common tests through your fixtures
© Copyright 2005, thycotic.
What about private What about private methods?methods?
• Test the features• Testable = Reusable• Code should be:
– Modular– Reusable– Very specific in function
© Copyright 2005, thycotic.
Testing Database CodeTesting Database Code
• Separate unit test database• Recreate a known database state
– Use a stored procedure or set of SQL
• Tests must be independent• Tests must be fast
© Copyright 2005, thycotic.
DatabaseScripterDatabaseScripter
• Free tool on thycotic.com !• Steps
– Create your test data– Run the DatabaseScripter– Use your SETUP stored procedure to
initialize your data
© Copyright 2005, thycotic.
Mock ObjectsMock Objects• Fake objects that replace real ones• Why use them?
– Avoid external dependencies– Reduce coupling– Keep tests fast– Test object interactions– Promote interface based design– Ensure tests are durable
• Example: Sending email?
© Copyright 2005, thycotic.
PsychologyPsychology
• TDD breaks complex problems down into manageable units
• Builds confidence with positive feedback
• Makes changing existing software easier
• Tests provide a positive feedback loop
© Copyright 2005, thycotic.
ConclusionConclusion
• Higher quality software• Greater confidence• Get “test infected”• You won’t want to work any other
way!
© Copyright 2005, thycotic.
Recommended Resources Recommended Resources (1/2)(1/2)
• Test Driven Development Kent BeckISBN 0321146530
• Test Driven DevelopmentDave AstelsISBN 0131016490
• Extreme Programming ExplainedKent BeckISBN 0201616416
• Extreme Programming InstalledRon Jeffries, Ann Andersonand Chet HendricksonISBN 0201708426
© Copyright 2005, thycotic.
Recommended Resources Recommended Resources (2/2)(2/2)
• Test-Driven Development in Microsoft .NETJames Newkirk, Alexei VorontsovISBN 0735619484
• Refactoring – Improving the design of existing codeMartin Fowler, et al.ISBN 0201485672
• Mailing ListsLook on Yahoo for “Test Driven Development” and “Extreme Programming”
© Copyright 2005, thycotic.
Slides and CodeSlides and Code
• http://www.thycotic.com/presentations.html
My BlogMy Blog
• http://weblogs.asp.net/jcogley