Advanced unit test in real
worldUser Group Italiano ALT.NET – http://ugialt.net
Agenda• Using Stubs to Break Dependencies • Interaction testing using Mock Objects • Working with legacy code• Test Hierarchies and organization • The pillars of good unit tests • Integrating unit testing into the organization • Design and Testability • Tools and frameworks • The future of unit test
Unit testing vs TDD• Title was:– Advanced tdd in real world
• but now it’s– Advanced unit test in real world
Unit Testing• It’s a methodology to verify single portions of
source code• A Unit is the minimal testable part:– Procedural coding: • Single program• function• procedure
– Object Oriented Programming• method
Test-Driven Development• It’s principally a design activity in which the
tests are written before the code• For each iteration :– red: define the attended behaviour whit assertions– green: all tests succeded and the assertion are
verified– refactor: improve the quality of code (applying
design patterns) keeping the tests green
Test-Driven Development in Microsoft .NET
• by James W. Newkirk and Alexei A. Vorontsov (April 14, 2004)
• http://www.amazon.com/Test-Driven-Development-Microsoft-NET-Professional/dp/0735619484/ref=sr_1_14?ie=UTF8&s=books&qid=1232141037&sr=8-14
Antibiotics & test driven developmentFonte: http://www.sviluppoagile.it/antibiotici-e-test-driven-development
Uno sviluppatore con cui ho terminato oggi un’importante fase di un progetto web mi ha detto:
[...] Io diffido di chi propone un rimedio come panacea di tutti i mali. Nei progetti open source che vedo utilizzare i test automatici io vedo ancora liste di bug lunghissime.
Affermazione che equivale più o meno a dire:
Ritengo gli antibiotici un immenso bluff. Sono decenni che vengono impiegati e ci sono ancora milioni di malati in ogni parte del mondo
Ok, ci ammaliamo ancora tutti e ancora ovunque, ma rinuncereste agli antibiotici?Gli antibiotici non ci regalano l’immunità totale, ma hanno abbattuto l’impatto dei batteri sulle
nostre vite. I test automatici non sono una panacea, ma abbattono il tempo speso a fare debugging - che è
sempre tempo perso, poiché non è passato a creare valore ma a verificarlo
Using Stubs to Break Dependencies
Stub• A stub is a replacement for
an existing dependency in the system, which your test can have control over.
• A stub will help you test your code without dealing with the dependency directly.
External Dependency • An external dependency is an
object in your system which your code under test interacts with, and over which you have no control
• Examples:• File System• Threads• Memory• Time • etc..
Interaction testing using Mock Objects
State-Based Testing• Also called “State
Verification”, we determine whether the exercised method worked correctly by examining the state of the system under test and its collaborators (dependencies) after the method was exercised.
Interaction Testing• Interaction Testing is the act
of testing how an object under test calls and receives input from other objects
Working with legacy code• Experience taken from the field:
– Once I left it was really easy to go back to the “old” mode of work – There was too much pressure to do other things than to devote
time to this learning task – It was proving really hard to write tests against existing code – It was next to impossible to refactor the existing code (or not
enough time to do it) – The learning curve was too high for most people – Some people didn’t want to change their design – People did not see clear and quick results from doing this – Tooling was getting in the way (or lack thereof) – We didn’t know where to begin.
Test Hierarchies and organization
• Unit tests are just an important part of the application as the production source code.
• Give careful thought to where the tests actually reside, both physically, and logically, in relation to the code under test.
The pillars of good unit tests• Trustworthy • Maintainable • Readable
Integrating unit testing into the organization
• Becoming the agent of change• Ways to succeed• Ways to fail
Design and Testability• Should I change the design of my code so that
it is more easily testable ?• How can Test-Driven Development help me ?• What I want to test ?
TOOLS AND FRAMEWORKSDeveloper, developer, developer
Test Frameworks• MS Test• NUnit• MbUnit • Gallio• XUnit• Pex
Mock Frameworks• Moq• Rhino Mocks• Typemock Isolator• Nmock• NUnit.Mocks
Dependency Injection and IoC Containers
• Common IServiceLocator Framework• Managed Extensibility Framework• StructureMap• Microsoft Unity• Castle Windsor• Spring.NET• Autofac• Ninject
Complete list: http://www.artofunittesting.com/IOC_Containers_Resources
Unit Test vNext ( maybe 2.0 ? )
• DSL with dynamic languages• Parametric Unit Test generation
http://channel9.msdn.com/pdc2008/TL51/
• Concurrency Unit Test http://channel9.msdn.com/pdc2008/TL58/