Introduction Unit testing Integration testing
Integration test in SharePoint
Allan Hvam Petersen
Codecamp, SharePoint User GroupIntroduction Unit testing Integration testing
Me
Education
B.Sc. in Computer Science
AP in Computer Science
SharePoint experience
SharePoint Developer at ProActive since 2007
Questions B Just ask
Q: Do you have any practical experiences with the topic?
A: No, testing has been a small spare time project.
Introduction
Unit testing Integration testing
Agenda
Software testing
Automated functional testing
Agenda
1
Introduction
Agenda
Software testing
Automated functional testing
2
Unit testing
Dependency inversion
Mocking
TypeMock Isolator
3Integration testing
Considerations
TypeMock Challenges
Future Plans
Introduction
Unit testing Integration testing
Agenda
Software testing
Automated functional testing
Software testing
Types
Functional testing
Performance testing
Usability testing
etc.
Functional testing
Manual testing
Introduction
Unit testing Integration testing
Agenda Software testing
Automated functional testing
Automated functional testing
Extent
Unit testing (Units in isolation)
Integration testing (Interaction between Modules)
System testing (All components)
Introduction Unit testing Integration testing 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
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 1
Using a testing framework and writing a unit test
Write tests before implementation
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
Dependency inversion
Goal
Decouple high-level components from low-level components
Patterns
Plugin
Service Locator
Dependency Injection
others?
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 2
Dependency inversion
Use of patterns and interfaces can make testing easier
Fakes vs. Stubs
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 2
Dependency inversion
Use of patterns and interfaces can make testing easier
Fakes vs. Stubs
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
Test Doubles
Fake (Simple implemnetation)
1 p u b l i c int M e t h o d () { r e t u r n 0; }
Stub (Records information)
1 p u b l i c int M e t h o d () { M e t h o d C a l l e d = t r u e; r e t u r n 0; }
Mock (Pre-programmed expectations)
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 3
Writing a mocking framework
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 3
Writing a mocking framework
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 4
Using a mocking framework
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 4
Using a mocking framework
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
Limitations of inheritance based mocking
Sealed classes
Non virtual methods
Static methods
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
TypeMock Isolator
Hooks into the CLR profile API and injects code runtime.
TypeMock API
1 var web = I s o l a t e . F a k e . I n s t a n c e < SPWeb > ( ) ;
2 I s o l a t e . S w a p . A l l I n s t a n c e s < SPWeb > ( ) . W i t h ( web );
3 I s o l a t e . S w a p . N e x t I n s t a n c e < SPWeb > ( ) . W i t h ( web );
4 I s o l a t e . N o n P u b l i c . W h e n C a l l e d ( web , " I n v a l i d a t e " ).
5 D o I n s t e a d ( c o n t e x t = > { r e t u r n; });
6 I s o l a t e . S w a p . C a l l s O n ( a ). W i t h C a l l s T o ( b );
Criticism: Too powerful
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 5
Testing an SharePoint application
MVP Pattern
Service Locator Pattern
TypeMock Isolator API
Introduction Unit testing Integration testing Dependency inversion Mocking TypeMock Isolator
DEMO 5
Testing an SharePoint application
MVP Pattern
Service Locator Pattern
TypeMock Isolator API
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Comparison
Unit
Integration
System
Speed
Fast
Medium
Slow
Mocking
Every
Some
None
Wrinting
Hard
Medium
Easy
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Integration testing
Approaches
Big Bang
Top-Down
Button-Up
Goals
Independent
Fast
Reproducible
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
First thoughts
New farm for each test
1
Reinstall script
2
VMware Snapshots
Concerns: cumbersome and slow
Mock SharePoint
1
Middle layer/SharePoint Repository
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Using Typemock Isolator for Integration Tests
1
1 u s i n g(var s i t e = new S P S i t e ( " h t t p :// i n t e g r a t i o n - test - s i t e " )
2 { 3 I s o l a t e . W h e n C a l l e d (()= > S P F a r m . L o c a l ). W i l l R e t u r n ( 4 s i t e . W e b A p p l i c a t i o n . F a r m ); 5 6 var c o n t e x t = S P C o n t e x t . G e t C o n t e x t ( s i t e ); 7 I s o l a t e . W h e n C a l l e d (()= > S P C o n t e x t . C u r r e n t ). W i l l R e t u r n ( c o n t e x t ); 8 9 // Arrange , Act , A s s e r t 10 }
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
SharePoint Integration Testing
Making the Split:
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
SharePoint Integration Testing
Making the Split:
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
SharePoint Configuration Database
1 [ G u i d ( " 3 B60B328 - F4D6 -4902 - BEC6 - E 9 0 4 1 7 9 F A 2 4 6 " )] 2 p u b l i c c l a s s S P P e r s i s t e d O b j e c t : ... 3 { 4 [ P e r s i s t e d ] 5 p r i v a t e H a s h t a b l e m _ P r o p e r t i e s ; 6 ... 1 < O b j e c t Id = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 2 C l a s s I d = " 674 da553 - ea77 -44 a3 - b9f8 - . . . " 3 P a r e n t I d = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 4 N a m e = " T y p e M o c k F a r m " ... > 5 < P r o p e r t i e s > 6 < o b j e c t t y p e = " ... S P F a r m ... " > 7 ... 8 < fld n a m e = " m _ P r o p e r t i e s " t y p e = " n u l l " / > 9 ... 10 < / o b j e c t > 11 < / P r o p e r t i e s > 12 < / O b j e c t >
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
SharePoint Configuration Database
1 [ G u i d ( " 3 B60B328 - F4D6 -4902 - BEC6 - E 9 0 4 1 7 9 F A 2 4 6 " )] 2 p u b l i c c l a s s S P P e r s i s t e d O b j e c t : ... 3 { 4 [ P e r s i s t e d ] 5 p r i v a t e H a s h t a b l e m _ P r o p e r t i e s ; 6 ... 1 < O b j e c t Id = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 2 C l a s s I d = " 674 da553 - ea77 -44 a3 - b9f8 - . . . " 3 P a r e n t I d = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 4 N a m e = " T y p e M o c k F a r m " ... > 5 < P r o p e r t i e s > 6 < o b j e c t t y p e = " ... S P F a r m ... " > 7 ... 8 < fld n a m e = " m _ P r o p e r t i e s " t y p e = " n u l l " / > 9 ... 10 < / o b j e c t > 11 < / P r o p e r t i e s > 12 < / O b j e c t >
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
SharePoint Configuration Database
1 [ G u i d ( " 3 B60B328 - F4D6 -4902 - BEC6 - E 9 0 4 1 7 9 F A 2 4 6 " )] 2 p u b l i c c l a s s S P P e r s i s t e d O b j e c t : ... 3 { 4 [ P e r s i s t e d ] 5 p r i v a t e H a s h t a b l e m _ P r o p e r t i e s ; 6 ... 1 < O b j e c t Id = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 2 C l a s s I d = " 674 da553 - ea77 -44 a3 - b9f8 - . . . " 3 P a r e n t I d = " 2 6 9 0 8 0 da -0041 -4 b34 - 9 1 7 c - . . . " 4 N a m e = " T y p e M o c k F a r m " ... > 5 < P r o p e r t i e s > 6 < o b j e c t t y p e = " ... S P F a r m ... " > 7 ...
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
TypeMock Challenges
Faking internal types
1 p u b l i c s t a t i c o b j e c t I n s t a n c e ( 2 t h i s I F a k e r faker , 3 T y p e t , 4 M e m b e r s b e h a v i o r ) 5 { 6 var f a k e r T y p e = t y p e o f( I F a k e r ); 7 var m e t h o d = f a k e r T y p e . G e t M e t h o d ( " I n s t a n c e " , 8 new[] { t y p e o f( M e m b e r s ) }); 9 var g e n e r i c M e t h o d = m e t h o d . M a k e G e n e r i c M e t h o d ( t ); 10 r e t u r n g e n e r i c M e t h o d . I n v o k e ( I s o l a t e . Fake , 11 new o b j e c t[] { b e h a v i o r }); 12 }
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
TypeMock Challenges
Dynamic new behavior, interception
1 u s i n g T y p e M o c k . I n t e r n a l . H o o k s ; 2 3 E v e n t T u n n e l . S h o u l d I n t e r c e p t D e c i s i o n = S h o u l d I n t e r c e p t D e c i s i o n ; 4 E v e n t T u n n e l . D u r i n g M e t h o d C a l l = D u r i n g M e t h o d C a l l ; 1 p r i v a t e s t a t i c v o i d S h o u l d I n t e r c e p t D e c i s i o n ( 2 T M S h o u l d I n t e r c e p t E v e n t A r g s e v e n t A r g s ) 3 { 4 e v e n t A r g s . R e t u r n s ( I n t e r c e p t B e h a v i o r . L e t I s o l a t o r D e c i d e ); 5 6 if ( e v e n t A r g s . M e t h o d N a m e == " . c t o r " ) 7 {
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
TypeMock Challenges
Dynamic new behavior, swapping
1 p r i v a t e s t a t i c v o i d D u r i n g M e t h o d C a l l ( 2 T M D u r i n g C a l l E v e n t A r g s e v e n t A r g s ) 3 { 4 if ( e v e n t A r g s . M e t h o d N a m e == " . c t o r " ) 5 { 6 D i s a b l e E v e n t F i r i n g (); 7 8 var obj = ... // M o c k e d o b j e c t 9 10 E n a b l e E v e n t F i r i n g (); 11 12 S w a p . C a l l s O n ( e v e n t A r g s . C o n t e x t ). W i t h C a l l s T o ( obj ); 13 14 e v e n t A r g s . R e t u r n s ( I s o l a t i o n B e h a v i o r . S k i p M e t h o d ); 15 }
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
DEMO 6
Testing an SharePoint application in-memory
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
DEMO 6
Testing an SharePoint application in-memory
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Current Status & Future Plans
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
Future Plans (if I get time)
Do Update() and SPSite lookup
Model the content databases
Mock the timer services?
Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Pex - Automated White box Testing for .NET
Peli de Halleux and Nikolai Tillmann: Unit Testing SharePoint
Services with Pex
2Moles
1 M S P S i t e . N e w S t r i n g = ( site , _ u r l ) = > { 2 new M S P S i t e ( s i t e ) { 3 O p e n W e b = () = > new M S P W e b () { 4 D i s p o s e = () = > { } , 5 L i s t s G e t = () = > new M S P L i s t C o l l e c t i o n () { 6 // ... 7 }. I n s t a n c e 8 }. I n s t a n c e 9 }; 10 };Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans
Pex - Automated White box Testing for .NET
Peli de Halleux and Nikolai Tillmann: Unit Testing SharePoint
Services with Pex
2Moles
1 M S P S i t e . N e w S t r i n g = ( site , _ u r l ) = > { 2 new M S P S i t e ( s i t e ) { 3 O p e n W e b = () = > new M S P W e b () { 4 D i s p o s e = () = > { } , 5 L i s t s G e t = () = > new M S P L i s t C o l l e c t i o n () { 6 // ... 7 }. I n s t a n c e 8 }. I n s t a n c e 9 };Introduction Unit testing Integration testing Considerations TypeMock Challenges Future Plans