gentle introduction to unit testing
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.TRANSCRIPT
Consulting/Training
Joel Cochran
Lead Developer, WintellectNOW.com
@joelcochran
A Gentle Introduction to Unit Testing
Consulting/Training
About Me
Microsoft Client Development MVPASPInsiderTwitter: @joelcochranEmail: [email protected]: joelcochran.comLead Developer, WintellectNOW
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
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
Consulting/Training
Interview with a Method
Consulting/Training
The Multiply Method Signature
Int32 Multiply(Int32[] values)
Consulting/Training
Question #1
What parameter(s) do you take?
Consulting/Training
Question #2
What value type do you return?
Consulting/Training
Question #3
What happens when I pass you:[3, 4]?
Consulting/Training
Question #4
What happens when I pass you:[7]?
Consulting/Training
Question #5
What happens when I pass you:[-5, 12, 10, -3]?
Consulting/Training
Question #6
What happens when I pass you:null?
Consulting/Training
Question #7
What happens when I pass you:[23, Int32.MaxValue]?
Consulting/Training
Question #8
You don’t seem to know much…
If you don’t know the answer to these questions, who does?
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
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
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
Consulting/Training
What Not to Test
Network connections
Database Connections
Email/SMTP
Web services
Other people’s code
Consulting/Training
Anatomy of a Unit Test
Arrange
• Assumptions
• Expectations
Act
• Execute• Results
Assert
• Compare• Report
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); }
Consulting/Training
Architecting for Unit Tests
Abstract external dependencies
Design to an Interface
Overloaded Constructors
Inversion of Control (IOC) Containers
Consulting/Training
Eating the Elephant: Unit Testing Legacy CodeIdentify “low hanging fruit”
Determine highest priority elements
Locate external dependencies
Be reasonable
Consulting/Training