24
Consulting/Training Joel Cochran Lead Developer, WintellectNOW.com [email protected] @joelcochran A Gentle Introduction to Unit Testing

Gentle Introduction To Unit Testing

Embed Size (px)

DESCRIPTION

By now you’ve heard of unit tests. Perhaps you’ve read an article about them or watched a video. Maybe you’ve even tried to write a few yourself, but the whole unit testing “thing” hasn’t quite hit home yet. Maybe you’ve been put off by the Testinistas or your company thinks unit tests are just a bunch of extra code. No matter where you are on this spectrum, if you aren’t writing unit tests as part of your daily coding, then this talk is for you. In this gentle introduction, we will discuss the value of unit tests. You will learn some practical techniques you can use to start to make unit testing part of your daily routine.

Citation preview

Page 1: Gentle Introduction To Unit Testing

Consulting/Training

Joel Cochran

Lead Developer, WintellectNOW.com

[email protected]

@joelcochran

A Gentle Introduction to Unit Testing

Page 2: Gentle Introduction To Unit Testing

Consulting/Training

About Me

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

Page 3: Gentle Introduction To Unit Testing

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: Gentle Introduction To Unit Testing

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: Gentle Introduction To Unit Testing

Consulting/Training

Interview with a Method

Page 6: Gentle Introduction To Unit Testing

Consulting/Training

The Multiply Method Signature

Int32 Multiply(Int32[] values)

Page 7: Gentle Introduction To Unit Testing

Consulting/Training

Question #1

What parameter(s) do you take?

Page 8: Gentle Introduction To Unit Testing

Consulting/Training

Question #2

What value type do you return?

Page 9: Gentle Introduction To Unit Testing

Consulting/Training

Question #3

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

Page 10: Gentle Introduction To Unit Testing

Consulting/Training

Question #4

What happens when I pass you:[7]?

Page 11: Gentle Introduction To Unit Testing

Consulting/Training

Question #5

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

Page 12: Gentle Introduction To Unit Testing

Consulting/Training

Question #6

What happens when I pass you:null?

Page 13: Gentle Introduction To Unit Testing

Consulting/Training

Question #7

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

Page 14: Gentle Introduction To Unit Testing

Consulting/Training

Question #8

You don’t seem to know much…

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

Page 15: Gentle Introduction To Unit Testing

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 16: Gentle Introduction To Unit Testing

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 17: Gentle Introduction To Unit Testing

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 18: Gentle Introduction To Unit Testing

Consulting/Training

What Not to Test

Network connections

Database Connections

Email/SMTP

Web services

Other people’s code

Page 19: Gentle Introduction To Unit Testing

Consulting/Training

Anatomy of a Unit Test

Arrange

• Assumptions

• Expectations

Act

• Execute• Results

Assert

• Compare• Report

Page 20: Gentle Introduction To Unit Testing

Consulting/Training

Sample Unit Test

[TestMethod] public void Add_PassedEmptyString_ShouldReturnZero() {     var calc = new StringCalculator();     var expected = 0;     int actual = calc.Add(String.Empty);     Assert.AreEqual(expected, actual); }

Page 21: Gentle Introduction To Unit Testing

Consulting/Training

Architecting for Unit Tests

Abstract external dependencies

Design to an Interface

Overloaded Constructors

Inversion of Control (IOC) Containers

Page 22: Gentle Introduction To Unit Testing

Consulting/Training

Eating the Elephant: Unit Testing Legacy CodeIdentify “low hanging fruit”

Determine highest priority elements

Locate external dependencies

Be reasonable

Page 23: Gentle Introduction To Unit Testing

Consulting/Training

Page 24: Gentle Introduction To Unit Testing

Consulting/Training

Questions?

Joel Cochran

[email protected]

@joelcochran