27
Unit Testing and Unit Testing and Test Driven Test Driven Development Development Presented By Presented By Greg Sohl Greg Sohl

Automated Unit Testing and TDD

Embed Size (px)

DESCRIPTION

Slides from a presentation I gave at CRineta (www.crineta.org) at its April '10 meeting.

Citation preview

Page 1: Automated Unit Testing and TDD

Unit Testing and Test Unit Testing and Test Driven DevelopmentDriven Development

Presented ByPresented By

Greg SohlGreg Sohl

Page 2: Automated Unit Testing and TDD

AgendaAgendaTesting? Dude, I already test.Testing? Dude, I already test.

Unit testing and TDD OverviewUnit testing and TDD Overview

Simple TDD exampleSimple TDD example Calculation ClassCalculation Class

But I already have codeBut I already have code

More complex example, with injectionMore complex example, with injection File Copy ClassFile Copy Class

ToolsTools

Q & AQ & A

Page 3: Automated Unit Testing and TDD

Testing? Dude, I Already TestTesting? Dude, I Already Test

Do you currently unit test your code?Do you currently unit test your code?

What types of unit tests do you create?What types of unit tests do you create?

CRineta Member Survey, 8/08

Page 4: Automated Unit Testing and TDD

Unit Testing and TDDUnit Testing and TDD

The process of designing software by writing a validating client before the code.

What is Test-Driven Development (TDD)?What is Test-Driven Development (TDD)?

Programmatically and continuously validating the correctness the code.

What is Automated Unit Testing?What is Automated Unit Testing?

Page 5: Automated Unit Testing and TDD

Unit Testing and TDDUnit Testing and TDD

Automated Unit Testing != Test-Driven DevelopmentAutomated Unit Testing != Test-Driven Development

TDD is a way of thinking about your code, but it doesn’t require automated unit testing.

Agile processes adapt to change. Unit tests help to manage change in our code.

Unit Testing is a key component of the Agile MethodologyUnit Testing is a key component of the Agile Methodology

Page 6: Automated Unit Testing and TDD

Unit TestingUnit Testing

Page 7: Automated Unit Testing and TDD

Writing Tests FirstWriting Tests First

Page 8: Automated Unit Testing and TDD

Testing - Before and AfterTesting - Before and After

Page 9: Automated Unit Testing and TDD

TDDTDD

Page 10: Automated Unit Testing and TDD

TDD DemoTDD Demo

Page 11: Automated Unit Testing and TDD

Test PartsTest Parts

The 3 A’s The 3 A’s Arrange, Act, AssertArrange, Act, Assert

[Test]Public void Send_SingleRecipient_Success{ EmailManager emailManager = new EmailManager();

bool result = emailManager.Send(“[email protected]”, “[email protected]”, “Hello”, “message”);

Assert.AreEqual(true, result, “The email was not successfully sent”);

}

<=Arrange

<=Act

<=Assert

Page 12: Automated Unit Testing and TDD

Sounds good, but…Sounds good, but…

Page 13: Automated Unit Testing and TDD

Misconception #1Misconception #1

Won’t writing unit tests increase Won’t writing unit tests increase development time?development time? Unit tests are an investment towards time Unit tests are an investment towards time

savings over the lifetime of the project.savings over the lifetime of the project. Provides regression testing when code Provides regression testing when code

changeschanges Find bugs earlyFind bugs early Good tests on fundamental areas saves Good tests on fundamental areas saves

integration testing timeintegration testing time

Page 14: Automated Unit Testing and TDD

Misconception #2Misconception #2

Won’t all my unit tests just get out of date?Won’t all my unit tests just get out of date? Unit tests are not parallel to your code like Unit tests are not parallel to your code like

documentation. You will be driving your code documentation. You will be driving your code with them.with them.

TDD is a programming paradigm.TDD is a programming paradigm. Unit tests are not an artifact of your code. Unit tests are not an artifact of your code.

They represent how you write it!They represent how you write it!

Page 15: Automated Unit Testing and TDD

Misconception #3Misconception #3

Won’t the client refuse to pay for it?Won’t the client refuse to pay for it? The client is paying for clean, correct code. The client is paying for clean, correct code.

Unit testing is the means to that end.Unit testing is the means to that end.

Do they pay us to test our code?Do they pay us to test our code? Do they pay us to use design patterns?Do they pay us to use design patterns? Do they pay us to debug our code?Do they pay us to debug our code?

Page 16: Automated Unit Testing and TDD

Unit Testing BenefitsUnit Testing BenefitsMakes you a user of your softwareMakes you a user of your software

Easier to find bugsEasier to find bugs Unless your tests have bugsUnless your tests have bugs

Easier to maintainEasier to maintain Except when changes require massive updates to Except when changes require massive updates to

teststests

Easier to understandEasier to understand Unless the tests are not well written / documentedUnless the tests are not well written / documented

Easier to developEasier to develop When tests are maintained as part of processWhen tests are maintained as part of process

Page 17: Automated Unit Testing and TDD

TDD Helps…TDD Helps…

Understanding the ProblemUnderstanding the Problem

Code CoverageCode Coverage

DesignDesign

Incremental DevelopmentIncremental Development

Page 18: Automated Unit Testing and TDD

Different Than Integration TestsDifferent Than Integration Tests

Doesn’t rely on external resourcesDoesn’t rely on external resources

Exercise only a single unit of codeExercise only a single unit of code

Page 19: Automated Unit Testing and TDD

Good Unit TestsGood Unit Tests

Page 20: Automated Unit Testing and TDD

Good Unit TestsGood Unit Tests

Page 21: Automated Unit Testing and TDD

Effective Unit TestingEffective Unit Testing

TrustworthyTrustworthy

MaintainableMaintainable

ReadableReadable

Page 22: Automated Unit Testing and TDD

Writing Testable CodeWriting Testable Code

Use interfaces Use interfaces

Dependency injectionDependency injection

Focused methodsFocused methods

Refactoring your code if it is not testableRefactoring your code if it is not testable

Page 23: Automated Unit Testing and TDD

Demo #2Demo #2

Page 24: Automated Unit Testing and TDD

Unit Testing ToolsUnit Testing Tools

nUnitnUnit

MS TestMS Test

mbUnitmbUnit

xUnitxUnit

Page 25: Automated Unit Testing and TDD

ReferencesReferences

Book: The Art of Unit Testing – Roy Book: The Art of Unit Testing – Roy OsheroveOsherove

Page 26: Automated Unit Testing and TDD

Other Useful InfoOther Useful Info

Database TestingDatabase Testing Article - Article - http://bit.ly/bVMvxq Article - Article - http://bit.ly/aHUMWW

Web Page TestingWeb Page Testing

WinForm TestingWinForm Testing WhiteWhite NUnitFormsNUnitForms

Page 27: Automated Unit Testing and TDD

QuestionsQuestions