© Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's...

32
© Copyright 2005, thycotic. Test Driven Development Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005

Transcript of © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's...

Page 1: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Test Driven DevelopmentTest Driven Development

Jonathan Cogley

Maryland Cold Fusion User's Group 10/11/2005

Page 2: © Copyright 2005, thycotic. Test 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

Page 3: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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 …

Page 4: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 5: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 6: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 7: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 8: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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?

Page 9: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 10: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 11: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 12: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 13: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 14: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Page 15: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 16: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

TDD DEMOTDD DEMO

• XP and TDD is best introduced by example

“pair with the new guy or gal”

Page 17: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Our TODO ListOur TODO List

• Try some basic sorting using a CFC– Audience participation essential!

Page 18: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

TDD TechniquesTDD Techniques

• Fake it

• Obvious Implementation

• Triangulation

Page 19: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Ensuring Quality TestsEnsuring Quality Tests

• Test according to requirements

• Statement coverage• Defect insertion

MetricsMetrics

Page 20: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 21: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

NUnit in a NutshellNUnit in a Nutshell

• NUnit.Framework namespace• [TestFixture]• [SetUp] [TearDown]• [Test]• [ExpectedException]• Assert class

– IsTrue– AreEqual

Page 22: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Other NUnit ExtensionsOther NUnit Extensions

• NUnitAddIn– Add-in for VS.NET

• NUnit task for the NAnt build tool• NUnitAsp

Page 23: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 24: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 25: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 26: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 27: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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?

Page 28: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 29: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

ConclusionConclusion

• Higher quality software• Greater confidence• Get “test infected”• You won’t want to work any other

way!

Page 30: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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

Page 31: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© 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”

Page 32: © Copyright 2005, thycotic. Test Driven Development Jonathan Cogley Maryland Cold Fusion User's Group 10/11/2005.

© Copyright 2005, thycotic.

Slides and CodeSlides and Code

• http://www.thycotic.com/presentations.html

My BlogMy Blog

• http://weblogs.asp.net/jcogley