Transcript
Page 1: Microsoft Fakes Framework - Unit Testing

Testing 2.0Unit Testing Microsoft Fake Framework & Unity IoC

16 Luglio 2014 @felicepescatore Disciplined Agile Delivery Italy Groupwww.felicepescatore.it

Page 2: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Agenda

• Test-Driven Development• Unit testing & TDD• Mock, stub e shim• Testing Framework• MS Fakes• Demo

Page 3: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Test-Driven Development

TDD: processo di scrittura del codice in cui lo sviluppo del software è guidato dalla stesura di test automatici.

Pensa(Scrivi i tests)

Red testsGreen tests

Refactor

“Tests must focus on the class' behavior, not its implementation.”

http://www.jamesshore.com/Agile-Book/test_driven_development.html

Page 4: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Testing Language

SUT - System Under Test

DOC - Depend-On Component

SMELL - "sentore di problema"

• Code Smell (problemi nel codice)

• Test Smell (problemi nel test)

Hollywood Principle: “Don’t call us; we’ll call you”,

alias per IoC alla base del funzionamento dei framework.

In un framework (a differenza delle librerie) l'invocazione

delle estensioni/personalizzazioni (hot sweet point)

viene comandato dal framework stesso, in funzione a

specifici eventi o allo scatenarsi di trigger.

•Librerie: il proprio codice chiama le librerie

•Framework: il framework chiama il proprio

codice di estension

Page 5: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Unit Testing & TDD

Unità Focus sulla classe o sul metodo

Isolamento

Separazione

Automatizzazione

Da sistemi o moduli esterni

Tra interfaccia e implementazione

Run ad ogni build

Page 6: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib6

Mock, stub e …

Mock

http://martinfowler.com/articles/mocksArentStubs.html

Stub

Oggetti preimpostati mediante calls expectations per effettuare la behaviour verification.

Oggetti che simulano la logica del sistema per effettuare la state verification.

FakesOggetti che implementano l’interfaccia dell’oggetto reale, implementandone parte della logica per effettuare la state verification.

Page 7: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

…shim!

Microsoft Windows Application Compatibility Toolkit (ACT)

WINE

Libreria o modulo software che in modo trasparente per l’utilizzatore:

• intercetta le chiamate ad una specifica API

• gestisce in autonomia la chiamata stessa

• trasforma i parametri

• redirige il flusso di esecuzione.

Page 8: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Stub o Mock?

Stub

Mock

WarehouseStub warehouse = new WarehouseStub();

warehouse.HasInventory = ()=>{return false};

order.fill(warehouse);

Assert.IsFalse(order.IsFilled());

WarehouseMock mock = new WarehouseMock(typeof(Warehouse));

warehouse.expects(once()).method(“HasInventory") .withAnyArguments() .will(returnValue(false));

order.fill(mock.Instance);

mock.VerifyExpectations();

Page 9: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib9

Framework/Lib .Net per unit testing

Framework Free/Commercial Features

xUnit.net Free (open source) Assertions, Attributes

SpecFlow Free (open source) Assertions, Specification DSL

NUnit Free (open source) Assertions, Attributes

csUnit Free (open source) Assertions, Attributes

MSTest Free Assertions, Attributes

Library Free/Commercial Mocks/Stubs

Typemock Isolator Commercial Mock

Telerik JustMock Commercial Mock

Rhino Mocks Free (open source) Mock/Stub

Moq Free (open source) Mock

MS Fakes Free Stub (mock?)/Fake

Page 10: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Microsoft Fakes Framework

MFF consente di isolare il codice di cui si sta eseguendo il test sostituendo moduli e/o librerie esterne mediante stubs o shims.

1. Inject interfaces

2. Add Fakes Assemblies

3. (Create a ShimContext) Construct instances of the stub/shim and provide code for its methods

Page 11: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

Demo

DEMO: MathLibAdvancedInject interfaces

Add Fakes Assemblies

Page 12: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

@felicepescatore

get in touch

Disciplined Agile Delivery Italy Group

Felice Pescatore, Agile Software

Architect

Email: [email protected]

Cell. 392/7157684

www.felicepescatore.it

12

ABOUT ME

Page 13: Microsoft Fakes Framework - Unit Testing

Unit Test – Fake lib

THANKS FOR WATCHING

Quest'opera è distribuita con Licenza Creative Commons Attribuzione - Non commerciale 3.0 Italia.


Top Related