Codecamp, SharePoint User Group October 31, 2009
Me Education B.Sc. in Computer Science AP in Computer Science SharePoint experience SharePoint Developer at ProActive since 2007 Questions Just ask Q: Do you have any practical experiences with the topic? A: No, testing has been a small spare time project. Q: How would you recommend testing SharePoint projects? A: For you to decide, and it depends on your needs.
Agenda Software testing Automated functional testing Agenda 1 Introduction Agenda Software testing Automated functional testing 2 Dependency inversion Mocking TypeMock Isolator 3
Agenda Software testing Automated functional testing Software testing Types Functional testing Performance testing Usability testing etc. Functional testing Manual testing Automated testing
Agenda Software testing Automated functional testing Automated functional testing Extent (Units in isolation) (Interaction between Modules) System testing (All components)
Dependency inversion Mocking TypeMock Isolator DEMO 1 Using a testing framework and writing a unit test
Dependency inversion Mocking TypeMock Isolator DEMO 1 Using a testing framework and writing a unit test Write tests before implementation Testing is easy and fun
Dependency inversion Mocking TypeMock Isolator Dependency inversion Goal Decouple high-level components from low-level components Patterns Plugin Service Locator Dependency Injection others?
Dependency inversion Mocking TypeMock Isolator DEMO 2 Dependency inversion
Dependency inversion Mocking TypeMock Isolator DEMO 2 Dependency inversion Use of patterns and interfaces can make testing easier Fakes vs. Stubs vs. unit testing
Dependency inversion Mocking TypeMock Isolator Test Doubles Fake (Simple implemnetation) 1 public int Method () { return 0; } Stub (Records information) 1 public int Method () { MethodCalled = true ; return 0; } Mock (Pre-programmed expectations) 1 public int Method () { Assert. Fail (); return 0; }
Dependency inversion Mocking TypeMock Isolator DEMO 3 Writing a mocking framework
Dependency inversion Mocking TypeMock Isolator DEMO 3 Writing a mocking framework Runtime code generation
Dependency inversion Mocking TypeMock Isolator DEMO 4 Using a mocking framework
Dependency inversion Mocking TypeMock Isolator DEMO 4 Using a mocking framework Some of the MOQ API
Dependency inversion Mocking TypeMock Isolator Limitations of inheritance based mocking Sealed classes Non virtual methods Static methods Depends on dependency inversion
Dependency inversion Mocking TypeMock Isolator TypeMock Isolator Hooks into the CLR profile API and injects code runtime. TypeMock API 1 var web = Isolate. Fake. Instance < SPWeb >(); 2 Isolate. Swap. AllInstances < SPWeb >(). With ( web ); 3 Isolate. Swap. NextInstance < SPWeb >(). With ( web ); 4 Isolate. NonPublic. WhenCalled (web, " Invalidate "). 5 DoInstead ( context => { return ; }); 6 Isolate. Swap. CallsOn (a). WithCallsTo (b); Criticism: Too powerful
Dependency inversion Mocking TypeMock Isolator DEMO 5 Testing an SharePoint application
Dependency inversion Mocking TypeMock Isolator DEMO 5 Testing an SharePoint application MVP Pattern Service Locator Pattern TypeMock Isolator API
Comparison Unit Integration System Speed Fast Medium Slow Mocking Every Some None Wrinting Hard Medium Easy Tradeoff between complexity and speed.
Approaches Big Bang Top-Down Button-Up Goals Independent Fast Reproducible
First thoughts New farm for each test 1 Reinstall script 2 VMware Snapshots Concerns: cumbersome and slow Mock SharePoint 1 Middle layer/sharepoint Repository 2 Mock all of the public API Concerns: huge project, won t run the real SharePoint logic
Using Typemock Isolator for Integration Tests 1 1 using ( var site = new SPSite (" http :// integration -test - site ") 2 { 3 Isolate. WhenCalled (()= > SPFarm. Local ). WillReturn ( 4 site. WebApplication. Farm ); 5 6 var context = SPContext. GetContext ( site ); 7 Isolate. WhenCalled (()= > SPContext. Current ). WillReturn ( context ); 8 9 // Arrange, Act, Assert 10 } What type of test is this? Problems? 1 http://blogs.msdn.com/francischeung/archive/2009/06/24/usingtypemock-isolator-for-integration-tests.aspx
SharePoint Integration Testing Making the Split:
SharePoint Integration Testing Making the Split: How does SharePoint find itself?
SharePoint Configuration Database
SharePoint Configuration Database 1 [ Guid ("3 B60B328 -F4D6-4902 - BEC6 - E904179FA246 ")] 2 public class SPPersistedObject :... 3 { 4 [ Persisted ] 5 private Hashtable m_properties ; 6...
SharePoint Configuration Database 1 [ Guid ("3 B60B328 -F4D6-4902 - BEC6 - E904179FA246 ")] 2 public class SPPersistedObject :... 3 { 4 [ Persisted ] 5 private Hashtable m_properties ; 6... 1 < Object Id=" 269080 da -0041-4 b34-917c -... " 2 ClassId =" 674 da553 -ea77-44 a3 -b9f8 -... " 3 ParentId =" 269080 da -0041-4 b34-917c -... " 4 Name =" TypeMock Farm "... > 5 < Properties > 6 < object type ="... SPFarm... "> 7... 8 <fld name =" m_properties " type =" null " / > 9... 10 </ object > 11 </ Properties > 12 </ Object >
Faking internal types 1 public static object Instance ( 2 this IFaker faker, 3 Type t, 4 Members behavior ) 5 { 6 var fakertype = typeof ( IFaker ); 7 var method = fakertype. GetMethod (" Instance ", 8 new [] { typeof ( Members ) }); 9 var genericmethod = method. MakeGenericMethod ( t); 10 return genericmethod. Invoke ( Isolate. Fake, 11 new object [] { behavior }); 12 }
Dynamic new behavior, interception 1 using TypeMock. Internal. Hooks ; 2 3 EventTunnel. ShouldInterceptDecision = ShouldInterceptDecision ; 4 EventTunnel. DuringMethodCall = DuringMethodCall ; 1 private static void ShouldInterceptDecision ( 2 TMShouldInterceptEventArgs eventargs ) 3 { 4 eventargs. Returns ( InterceptBehavior. LetIsolatorDecide ); 5 6 if ( eventargs. MethodName == ". ctor ") 7 { 8 eventargs. Returns ( InterceptBehavior. Intercept ); 9 } 10 }
Dynamic new behavior, swapping 1 private static void DuringMethodCall ( 2 TMDuringCallEventArgs eventargs ) 3 { 4 if ( eventargs. MethodName == ". ctor ") 5 { 6 DisableEventFiring (); 7 8 var obj =... // Mocked object 9 10 EnableEventFiring (); 11 12 Swap. CallsOn ( eventargs. Context ). WithCallsTo ( obj ); 13 14 eventargs. Returns ( IsolationBehavior. SkipMethod ); 15 } 16 }
DEMO 6 Testing an SharePoint application in-memory
DEMO 6 Testing an SharePoint application in-memory Can be easy if you got the right tools/frameworks
Current Status & Current status Can read most objects from the configuration database Found 3 bugs in TypeMock during development, none fixed The bugs prevent me from Update() and SPSite lookup (if I get time) Do Update() and SPSite lookup Model the content databases Mock the timer services? Mock the filesystem?
Pex - Automated White box Testing for.net Peli de Halleux and Nikolai Tillmann: Unit Testing SharePoint Services with Pex 2 2 http://research.microsoft.com/en-us/projects/pex/pexsharepoint.pdf
Pex - Automated White box Testing for.net Peli de Halleux and Nikolai Tillmann: Unit Testing SharePoint Services with Pex 2 Moles 1 MSPSite. NewString = ( site, _url ) => { 2 new MSPSite ( site ) { 3 OpenWeb = () => new MSPWeb () { 4 Dispose = () => { }, 5 ListsGet = () => new MSPListCollection () { 6 //... 7 }. Instance 8 }. Instance 9 }; 10 }; 2 http://research.microsoft.com/en-us/projects/pex/pexsharepoint.pdf
End Questions?