Upload
kleinron
View
5.141
Download
5
Tags:
Embed Size (px)
DESCRIPTION
What it unit testing, what's a unit, how to test?
Citation preview
Unit Testing with NUnitRon Kleinhttp://heblog.ronklein.co.il http://tinyurl.com/ronklein
Agenda Introduction to Unit Testing Basic Examples with NUnit Why Unit Testing? Advanced Features (if we have time)
Code
Tests
Tests
Tests
Introduction to Unit Testing
What's a Unit?Unit == class || methodUnit != classes.Count > 1
How to test?Manually? –possible, but not so friendlyAutomated Frameworks:
NUnitMSTestMbUnitetc.
Testing FrameworkA set of code and tools to test existing codeTesting code is decorated with attributesTesting Framework uses Reflection to invoke testing code
Most tests either Pass or Fail
Demo – Array UtilThe task:A method has 2 input parameters:
A non empty, sorted array of integersAn integer
The output should be the closest number from the array to the input number.
"Closest" in terms of absolute valueTry to write it yourself!
Why Unit Testing?Unit Testing is a STANDARD
No need to manually test code by console applications
Accepted by Developers Expected by Developers
Fearlessly Change Implementation
Why Unit Testing?Fearlessly Change Implementation(back to demo)
Why Unit Testing?Leads us to Better Code / Better DesignDemo: Greet example
I Can't Test That CodeMore than a single class is used
Even worse: Singletons are usedHardware boundaries:
System ClockHTTP RequestsFile Read/Write
Refactor!
Unit TestingMore code to test our codeUnit Testing Leads to:
Fearlessly Change ImplementationBetter Code / Better Design
If a class can't be tested – consider refactoring
Legacy code is code without tests
Resources
NUnit: http://nunit.org/ Moq: http://code.google.com/p/moq/
Thank You!