intro to unit testing with test driven development

24
Consulting/Training Joel Cochran Lead Developer, WintellectNOW.com [email protected] @joelcochran Intro to Test Driven Development (TDD)

Upload: joel-cochran

Post on 22-Jun-2015

124 views

Category:

Software


3 download

DESCRIPTION

Test Driven Development is a technique that requires Unit Tests for code to be written before the code itself. By placing the emphasis on testing, developers can define success before code is written. This helps us answer that elusive question of "when is code done?" TDD offers additional benefits such as only writing code you need to satisfy your requirements (YAGNI), and enhanced maintainability.

TRANSCRIPT

Page 1: Intro to Unit Testing with test Driven Development

Consulting/Training

Joel Cochran

Lead Developer, WintellectNOW.com

[email protected]

@joelcochran

Intro to Test Driven Development (TDD)

Page 2: Intro to Unit Testing with test Driven Development

Consulting/Training

About Me

Microsoft Client Development MVPASPInsiderTwitter: @joelcochranEmail: [email protected]: joelcochran.comLead Developer, WintellectNOW

Page 3: Intro to Unit Testing with test Driven Development

Consulting/Training

About Wintellect

Founded by top experts on Microsoft – Jeffrey Richter, Jeff Prosise, and John Robbins – we pull out all the stops to help our customers achieve their goals through advanced software-based consulting and training solutions.

consulting

Wintellect helps you build better software, faster, tackling the tough projects and solving the software and technology questions that help you transform your business.

Architecture, Analysis and Design Full lifecycle software development Debugging and Performance tuning Database design and development

training

Wintellect's courses are written and taught by some of the biggest and most respected names in the Microsoft programming industry.

Learn from the best. Access the same training Microsoft’s developers enjoy

Real world knowledge and solutions on both current and cutting edge technologies

Flexibility in training options – onsite, virtual, on demand

who we are

Page 4: Intro to Unit Testing with test Driven Development

Consulting/TrainingIndividuals | Businesses | Enterprise Organizations

Authors Enjoy:

Royalty Income

Personal Branding

Cross-Sell Opps

Free library access

Subscribers Enjoy:

Expert Instructors

Quality Content

Practical Application

All Devices

Try it freeCode: <INSERT CODE

HERE>WintellectNOW.co

m

Wintellect’s On-Demand Video Training Solution

COCHRAN-13

Page 5: Intro to Unit Testing with test Driven Development

Consulting/Training

Interview with a Method

Page 6: Intro to Unit Testing with test Driven Development

Consulting/Training

The Multiply Method Signature

Int32 Multiply(Int32[] values)

Page 7: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #1

What parameter(s) do you take?

An Array of Int32

Page 8: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #2

What value type do you return?

Int32

Page 9: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #3

What happens when I pass you:[3, 4]?

I don’t know

Page 10: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #4

What happens when I pass you:[7]?

I don’t know

Page 11: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #5

What happens when I pass you:[-5, 12, 10, -3]?

I don’t know

Page 12: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #6

What happens when I pass you:null?

I don’t know

Page 13: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #7

What happens when I pass you:[23, Int32.MaxValue]?

I don’t know

Page 14: Intro to Unit Testing with test Driven Development

Consulting/Training

Question #8

You don’t seem to know much…

If you don’t know the answer to these questions, who does?

The Unit Tests

Page 15: Intro to Unit Testing with test Driven Development

Consulting/Training

What is a Unit Test?

Code that tests code

Small discrete chunks (units)

One test, one condition

Based on assumptions

Should be automated and repeatable

Page 16: Intro to Unit Testing with test Driven Development

Consulting/Training

What a Unit Test is Not

An end-to-end test

Connected to live data

Performing external functions (like sending an Email)

User tests

Page 17: Intro to Unit Testing with test Driven Development

Consulting/Training

Why write Unit Tests?

Test code to ensure proper functionality given certain assumptions

Exercise code without running the app

Write once, run a million times

Guards against unintended consequences

Tests define success

Page 18: Intro to Unit Testing with test Driven Development

Consulting/Training

What is TDD?

Unit Tests are written before the code

Helps shape the code to the task at hand

Provides focus (YAGNI)

The test defines success

Run all tests every time

Page 19: Intro to Unit Testing with test Driven Development

Consulting/Training

TDD Pattern

REDwrite a failing test

GREEN write enough code to

pass

REFACTORengineer the code

Page 20: Intro to Unit Testing with test Driven Development

Consulting/Training

Anatomy of a Unit Test

Arrange

•Assumptions•Expectations

Act•Execute•Results

Assert•Compare•Report

Page 21: Intro to Unit Testing with test Driven Development

Consulting/Training

TDD Kata

Daily exercise – 30 minutes or less

Roy Osherove, author of “The Art of Unit Testing”, Manning Publications

http://osherove.com/tdd-kata-1/

Let’s code!

Page 22: Intro to Unit Testing with test Driven Development

Consulting/Training

The String Calculator Kata

Page 23: Intro to Unit Testing with test Driven Development

Consulting/Training

StringCalculator Kata

Page 24: Intro to Unit Testing with test Driven Development

Consulting/Training

Questions?

Joel Cochran

[email protected]

@joelcochran