19
Advanced unit test in real world User Group Italiano ALT.NET – http://ugialt.net

Advanced unit test in real world

  • Upload
    avari

  • View
    58

  • Download
    1

Embed Size (px)

DESCRIPTION

Advanced unit test in real world. User 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 - PowerPoint PPT Presentation

Citation preview

Page 1: Advanced unit test in real world

Advanced unit test in real

worldUser Group Italiano ALT.NET – http://ugialt.net

Page 2: Advanced unit test in real world

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

Page 3: Advanced unit test in real world

Unit testing vs TDD• Title was:– Advanced tdd in real world

• but now it’s– Advanced unit test in real world

Page 4: 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

Page 5: Advanced unit test in real world

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

Page 6: Advanced unit test in real world

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

Page 7: Advanced unit test in real world

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

Page 8: Advanced unit test in real world

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..

Page 9: Advanced unit test in real world

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

Page 10: Advanced unit test in real world

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.

Page 11: Advanced unit test in real world

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.

Page 12: Advanced unit test in real world

The pillars of good unit tests• Trustworthy • Maintainable • Readable

Page 13: Advanced unit test in real world

Integrating unit testing into the organization

• Becoming the agent of change• Ways to succeed• Ways to fail

Page 14: Advanced unit test in real world

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 ?

Page 15: Advanced unit test in real world

TOOLS AND FRAMEWORKSDeveloper, developer, developer

Page 16: Advanced unit test in real world

Test Frameworks• MS Test• NUnit• MbUnit • Gallio• XUnit• Pex

Page 17: Advanced unit test in real world

Mock Frameworks• Moq• Rhino Mocks• Typemock Isolator• Nmock• NUnit.Mocks

Page 18: Advanced unit test in real world

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

Page 19: Advanced unit test in real world

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/