Automated Unit Testing and TDD

Post on 30-Nov-2014

2.837 views 3 download

description

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

Transcript of Automated Unit Testing and TDD

Unit Testing and Test Unit Testing and Test Driven DevelopmentDriven Development

Presented ByPresented By

Greg SohlGreg Sohl

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

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

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?

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

Unit TestingUnit Testing

Writing Tests FirstWriting Tests First

Testing - Before and AfterTesting - Before and After

TDDTDD

TDD DemoTDD Demo

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(“johndoe@home.com”, “sammy@away.net”, “Hello”, “message”);

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

}

<=Arrange

<=Act

<=Assert

Sounds good, but…Sounds good, but…

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

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!

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?

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

TDD Helps…TDD Helps…

Understanding the ProblemUnderstanding the Problem

Code CoverageCode Coverage

DesignDesign

Incremental DevelopmentIncremental Development

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

Good Unit TestsGood Unit Tests

Good Unit TestsGood Unit Tests

Effective Unit TestingEffective Unit Testing

TrustworthyTrustworthy

MaintainableMaintainable

ReadableReadable

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

Demo #2Demo #2

Unit Testing ToolsUnit Testing Tools

nUnitnUnit

MS TestMS Test

mbUnitmbUnit

xUnitxUnit

ReferencesReferences

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

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

QuestionsQuestions