14
Unit Testing with NUnit Ron Klein http://heblog.ronklein.co.il http://tinyurl.com/ronklein

Unit testing with NUnit

Embed Size (px)

DESCRIPTION

What it unit testing, what's a unit, how to test?

Citation preview

Page 1: Unit testing with NUnit

Unit Testing with NUnitRon Kleinhttp://heblog.ronklein.co.il http://tinyurl.com/ronklein

Page 2: Unit testing with NUnit

Agenda Introduction to Unit Testing Basic Examples with NUnit Why Unit Testing? Advanced Features (if we have time)

Page 3: Unit testing with NUnit

Code

Tests

Tests

Tests

Introduction to Unit Testing

Page 4: Unit testing with NUnit

What's a Unit?Unit == class || methodUnit != classes.Count > 1

Page 5: Unit testing with NUnit

How to test?Manually? –possible, but not so friendlyAutomated Frameworks:

NUnitMSTestMbUnitetc.

Page 6: Unit testing with NUnit

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

Page 7: Unit testing with NUnit

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!

Page 8: Unit testing with NUnit

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

Page 9: Unit testing with NUnit

Why Unit Testing?Fearlessly Change Implementation(back to demo)

Page 10: Unit testing with NUnit

Why Unit Testing?Leads us to Better Code / Better DesignDemo: Greet example

Page 11: Unit testing with NUnit

I Can't Test That CodeMore than a single class is used

Even worse: Singletons are usedHardware boundaries:

System ClockHTTP RequestsFile Read/Write

Refactor!

Page 12: Unit testing with NUnit

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

Page 13: Unit testing with NUnit

Resources

NUnit: http://nunit.org/ Moq: http://code.google.com/p/moq/

Page 14: Unit testing with NUnit

Thank You!