SlideShare a Scribd company logo
Unit Testing in
SharePoint 2010
      Chris Weldon
   Dallas TechFest 2011
Before We Begin


http://slidesha.re/       http://spkr8.com/t/8140




git://github.com/neraath/testing-in-sp2010.git
Your Guide: Chris Weldon

•   Fightin’ Texas Aggie

•   .Net and PHP Developer

•   UNIX and Windows Sysadmin

•   Sr. Consultant at Improving Enterprises

•   chris@chrisweldon.net
Agile, Microsoft, Open Technologies, UX
Applied Training, Coaching, Mentoring
Certified Consulting
Rural Sourcing
Recruiting Services
Assumptions


• You develop software
• You unit test
• You know the SharePoint API (or can follow along)
Standard Testing Practices
                        A Common Problem
namespace SharePointLogic
{
    public class ClassWithDependencies
    {
        private IDataRepository repository;

        public ClassWithDependencies(IDataRepository repo)
        {
            this.repository = repo;
        }

        public INode GetNodeByName(string name)
        {
            return this.repository.Where(x => x.Name.Equals(name))
                                  .Select();
        }
    }
}
public IList<SPUser> GetUsersInSiteCollection(string siteUrl) {
  using (SPSite site = new SPSite(siteUrl) {
    site.CatchAccessDeinedError = false;

        using (SPWeb web = site.RootWeb) {
          try {
            if (web.DoesUserHavePermissions(SPBasePermissions.ManageWeb)) {
              SPUsercollection users = web.Users;
              List<SPUser> usersWithAccess = new List<SPUser>();
              foreach (SPUser user in users) {
                if (web.DoesUserHavePermissions(
                  user.LoginName, SPBasePermissions.ManageWeb)) {
                  usersWithAccess.Add(user);
                }
              }

                return usersWithAccess;
              }
            } catch (UnauthorizedAccessException e) {
              return new List<SPUser>();
            }
        }
    }
}
Standard Testing Practices
                A Common Problem



• What if no database locally?
• Did you write test logic to rebuild the database
  before each test run?

• What if this dependency is a physical piece of
  hardware?
Standard Testing Practices
                               How to Solve
namespace SharePointLogicTests
{
    [TestClass]
    public class ClassWithDependenciesTests
    {
        [TestMethod]
        public void TestGetNodesByName()
        {
            // Arrange.
            INode node = new SharePointNode();
            IDataRepository repository =
                (IDataRepository)MockRepository.Stub<IDataRepository>();
            repository.Stub(x => x.Where).Returns(repository);
            repository.Stub(x => x.Select).Returns(node);
            repository.Replay();

// ...
Standard Testing Practices
                                 How to Solve


            // Act.
            ClassWithDependencies testClass = new ClassWithDependencies(repository);
            INode testNode = testClass.GetNodeByName("Test");

            // Assert.
            Assert.AreEqual(node, testNode);
        }
    }
}
Standard Testing Practices
              What about SharePoint?


• Most of SharePoint object model has NO interfaces
• Worse, most also are Sealed classes, meaning no
  extending and overriding the SharePoint behavior

• Most SharePoint objects require active connection
  and instance of SharePoint on local server

 • Unlike database projects, resetting and recreating
    state in SharePoint is way more difficult
Standard Testing Practices
          A SharePoint Common Problem


public string GetNameOfSharePointWebSite(string url)
{
    using (SPSite site = new SPSite(url))
    {
        using (SPWeb web = site.OpenWeb())
        {
            return web.Name;
        }
    }
}
Pex and Moles
• What is it?
 • Pex discovers boundary conditions of your tests
    and automates test creation

 • Visual Studio Add In
 • Developed by Microsoft Research
 • Available for Academic or for MSDN subscribers
Pex and Moles

• Moles is, simply put, a mocking and stubbing
  framework

 • Different than other traditional mocking
    frameworks

 • Uses detours to custom delegates via runtime
    instrumentation
Pex and Moles


• Pex and Moles are not mutually exclusive
• Neither are dependent upon one another
DEMO

SharePoint & Moles
     Example
Pex and Moles
• As your dependency on SharePoint Object Model
  grows, more detours required for testing

• In most cases, more tedious than beneficial
• If only could have most of the basic SharePoint
  behaviors pre-generated

• Solution:
  Microsoft.SharePoint.Behaviors
DEMO

SharePoint Mole
   Behaviors
Observations
Observations
• Even behaviors are not complete
Observations
• Even behaviors are not complete
• A lot of mocking activity may get repeated
Observations
• Even behaviors are not complete
• A lot of mocking activity may get repeated
• Unit test setup logic gets refactored into common
  assemblies
Observations
• Even behaviors are not complete
• A lot of mocking activity may get repeated
• Unit test setup logic gets refactored into common
  assemblies

• Save time: create scenarios
Observations
• Even behaviors are not complete
• A lot of mocking activity may get repeated
• Unit test setup logic gets refactored into common
  assemblies

• Save time: create scenarios
• Use BDD-style approach for testing
Gotchas
Gotchas
• More moles unit tests = much longer test execution
  time
Gotchas
• More moles unit tests = much longer test execution
  time

• Continuous Integration
Gotchas
• More moles unit tests = much longer test execution
  time

• Continuous Integration
• This will take time to learn, research, and debug
Gotchas
• More moles unit tests = much longer test execution
  time

• Continuous Integration
• This will take time to learn, research, and debug
 • Pay attention when it stops adding value
Gotchas
• More moles unit tests = much longer test execution
  time

• Continuous Integration
• This will take time to learn, research, and debug
 • Pay attention when it stops adding value
• The GAC
Gotchas
• More moles unit tests = much longer test execution
  time

• Continuous Integration
• This will take time to learn, research, and debug
 • Pay attention when it stops adding value
• The GAC
Recommendations
Recommendations

• If you can do it, build a facade in front of SharePoint
Recommendations

• If you can do it, build a facade in front of SharePoint
• Make sure you are producing consistent behaviors
Recommendations

• If you can do it, build a facade in front of SharePoint
• Make sure you are producing consistent behaviors
 • If you don’t know what SharePoint does,
    disassemble it
Q&A
Thank You!


http://slidesha.re/       http://spkr8.com/t/8140




git://github.com/neraath/testing-in-sp2010.git

More Related Content

What's hot (20)

PDF
Introduction to jest
pksjce
 
PPTX
2014 Joker - Integration Testing from the Trenches
Nicolas Fränkel
 
PPTX
Quickly testing legacy code
Clare Macrae
 
PDF
How to Un-Flake Flaky Tests - A New Hire's Toolkit
Zachary Attas
 
PDF
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
PPTX
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
PPTX
Mini training - Moving to xUnit.net
Betclic Everest Group Tech Team
 
PPTX
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Steven Smith
 
PPTX
Improving the Quality of Existing Software
Steven Smith
 
PDF
How To Use Selenium Successfully
Dave Haeffner
 
PPTX
Quickly Testing Legacy Code - ACCU York April 2019
Clare Macrae
 
PDF
Mini-Training: NFluent
Betclic Everest Group Tech Team
 
PPTX
Automated Software Testing
arild2
 
ODP
Testing JSF with Arquillian and Selenium
Lukáš Fryč
 
PDF
DSR Testing (Part 1)
Steve Upton
 
PDF
Faking Hell
Giovanni Asproni
 
PDF
Functional tests for dummies
cpsitgmbh
 
PDF
DSR Microservices (Day 1, Part 2)
Steve Upton
 
PDF
Implementing Quality on a Java Project
Vincent Massol
 
PDF
DSR Testing (Part 2)
Steve Upton
 
Introduction to jest
pksjce
 
2014 Joker - Integration Testing from the Trenches
Nicolas Fränkel
 
Quickly testing legacy code
Clare Macrae
 
How to Un-Flake Flaky Tests - A New Hire's Toolkit
Zachary Attas
 
Adding unit tests with tSQLt to the database deployment pipeline
Eduardo Piairo
 
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Mini training - Moving to xUnit.net
Betclic Everest Group Tech Team
 
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Steven Smith
 
Improving the Quality of Existing Software
Steven Smith
 
How To Use Selenium Successfully
Dave Haeffner
 
Quickly Testing Legacy Code - ACCU York April 2019
Clare Macrae
 
Mini-Training: NFluent
Betclic Everest Group Tech Team
 
Automated Software Testing
arild2
 
Testing JSF with Arquillian and Selenium
Lukáš Fryč
 
DSR Testing (Part 1)
Steve Upton
 
Faking Hell
Giovanni Asproni
 
Functional tests for dummies
cpsitgmbh
 
DSR Microservices (Day 1, Part 2)
Steve Upton
 
Implementing Quality on a Java Project
Vincent Massol
 
DSR Testing (Part 2)
Steve Upton
 

Viewers also liked (11)

PPTX
Unit Testing SharePoint Applications
Gil Zilberfeld
 
PPTX
Managesp 160805190411
Danielle Jennings
 
PPTX
Risk Management in SharePoint Governance
Christian Buckley
 
PPTX
V Greavu - Testing with Sharepoint
TestCampRO
 
PPTX
Case Study for a SharePoint SDLC
Marie-Michelle Strah, PhD
 
PPT
Agile SharePoint Development With Scrum
Andrew Woodward
 
PDF
Information architecture and SharePoint
Lulu Pachuau
 
PPT
An SDLC for SharePoint
gvaughan
 
PPTX
Testing SharePoint solutions overview
Spiffy
 
PDF
Quality Assurance in SDLC
Adil Mughal
 
PPTX
Deep Dive into SharePoint Topologies and Server Architecture for SharePoint 2013
K.Mohamed Faizal
 
Unit Testing SharePoint Applications
Gil Zilberfeld
 
Managesp 160805190411
Danielle Jennings
 
Risk Management in SharePoint Governance
Christian Buckley
 
V Greavu - Testing with Sharepoint
TestCampRO
 
Case Study for a SharePoint SDLC
Marie-Michelle Strah, PhD
 
Agile SharePoint Development With Scrum
Andrew Woodward
 
Information architecture and SharePoint
Lulu Pachuau
 
An SDLC for SharePoint
gvaughan
 
Testing SharePoint solutions overview
Spiffy
 
Quality Assurance in SDLC
Adil Mughal
 
Deep Dive into SharePoint Topologies and Server Architecture for SharePoint 2013
K.Mohamed Faizal
 
Ad

Similar to Unit Testing in SharePoint 2010 (20)

PPTX
SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio
NCCOMMS
 
PDF
Unit Testing Best Practices
Tomaš Maconko
 
PPTX
Share Point Development With Unit Testing
Jeremy Thake
 
PDF
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
PDF
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware
 
PPTX
Writing Testable Code in SharePoint
Tim McCarthy
 
PPTX
Refactoring Away from Test Hell
Alastair Smith
 
PPTX
Sharepoint Saturday India Online best practice for developing share point sol...
Shakir Majeed Khan
 
PPTX
Building unit tests correctly with visual studio 2013
Dror Helper
 
PPTX
Ria 04 & 05 - First ASP.NET MVC project
Johannes Hoppe
 
PPTX
Building unit tests correctly
Dror Helper
 
PPTX
Tdd & unit test
GomathiNayagam S
 
PPTX
Unit tests = maintenance hell ?
Thibaud Desodt
 
PPTX
.Net Unit Testing with Visual Studio 2010
kgayda
 
PPTX
Testing with VS2010 - A Bugs Life
Peter Gfader
 
PDF
Comment j'ai mis ma suite de tests au régime en 5 minutes par jour
CARA_Lyon
 
PDF
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
PPTX
Principles and patterns for test driven development
Stephen Fuqua
 
PDF
Unit testing (workshop)
Foyzul Karim
 
PDF
Testing and TDD - KoJUG
lburdz
 
SPCA2013 - Test-driven Development with SharePoint 2013 and Visual Studio
NCCOMMS
 
Unit Testing Best Practices
Tomaš Maconko
 
Share Point Development With Unit Testing
Jeremy Thake
 
Breaking Dependencies to Allow Unit Testing
Steven Smith
 
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
FalafelSoftware
 
Writing Testable Code in SharePoint
Tim McCarthy
 
Refactoring Away from Test Hell
Alastair Smith
 
Sharepoint Saturday India Online best practice for developing share point sol...
Shakir Majeed Khan
 
Building unit tests correctly with visual studio 2013
Dror Helper
 
Ria 04 & 05 - First ASP.NET MVC project
Johannes Hoppe
 
Building unit tests correctly
Dror Helper
 
Tdd & unit test
GomathiNayagam S
 
Unit tests = maintenance hell ?
Thibaud Desodt
 
.Net Unit Testing with Visual Studio 2010
kgayda
 
Testing with VS2010 - A Bugs Life
Peter Gfader
 
Comment j'ai mis ma suite de tests au régime en 5 minutes par jour
CARA_Lyon
 
Test and Behaviour Driven Development (TDD/BDD)
Lars Thorup
 
Principles and patterns for test driven development
Stephen Fuqua
 
Unit testing (workshop)
Foyzul Karim
 
Testing and TDD - KoJUG
lburdz
 
Ad

More from Chris Weldon (7)

ODP
Keat presentation
Chris Weldon
 
KEY
REST Easy - Building RESTful Services in Zend Framework
Chris Weldon
 
KEY
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Chris Weldon
 
KEY
SOLID - Not Just a State of Matter, It's Principles for OO Propriety
Chris Weldon
 
KEY
SOLID Principles
Chris Weldon
 
KEY
IoC with PHP
Chris Weldon
 
PDF
PHP & MVC
Chris Weldon
 
Keat presentation
Chris Weldon
 
REST Easy - Building RESTful Services in Zend Framework
Chris Weldon
 
Beyond TDD: Enabling Your Team to Continuously Deliver Software
Chris Weldon
 
SOLID - Not Just a State of Matter, It's Principles for OO Propriety
Chris Weldon
 
SOLID Principles
Chris Weldon
 
IoC with PHP
Chris Weldon
 
PHP & MVC
Chris Weldon
 

Recently uploaded (20)

PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
What Makes Contify’s News API Stand Out: Key Features at a Glance
Contify
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
POV_ Why Enterprises Need to Find Value in ZERO.pdf
darshakparmar
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 

Unit Testing in SharePoint 2010

  • 1. Unit Testing in SharePoint 2010 Chris Weldon Dallas TechFest 2011
  • 2. Before We Begin http://slidesha.re/ http://spkr8.com/t/8140 git://github.com/neraath/testing-in-sp2010.git
  • 3. Your Guide: Chris Weldon • Fightin’ Texas Aggie • .Net and PHP Developer • UNIX and Windows Sysadmin • Sr. Consultant at Improving Enterprises • [email protected]
  • 4. Agile, Microsoft, Open Technologies, UX Applied Training, Coaching, Mentoring Certified Consulting Rural Sourcing Recruiting Services
  • 5. Assumptions • You develop software • You unit test • You know the SharePoint API (or can follow along)
  • 6. Standard Testing Practices A Common Problem namespace SharePointLogic { public class ClassWithDependencies { private IDataRepository repository; public ClassWithDependencies(IDataRepository repo) { this.repository = repo; } public INode GetNodeByName(string name) { return this.repository.Where(x => x.Name.Equals(name)) .Select(); } } }
  • 7. public IList<SPUser> GetUsersInSiteCollection(string siteUrl) { using (SPSite site = new SPSite(siteUrl) { site.CatchAccessDeinedError = false; using (SPWeb web = site.RootWeb) { try { if (web.DoesUserHavePermissions(SPBasePermissions.ManageWeb)) { SPUsercollection users = web.Users; List<SPUser> usersWithAccess = new List<SPUser>(); foreach (SPUser user in users) { if (web.DoesUserHavePermissions( user.LoginName, SPBasePermissions.ManageWeb)) { usersWithAccess.Add(user); } } return usersWithAccess; } } catch (UnauthorizedAccessException e) { return new List<SPUser>(); } } } }
  • 8. Standard Testing Practices A Common Problem • What if no database locally? • Did you write test logic to rebuild the database before each test run? • What if this dependency is a physical piece of hardware?
  • 9. Standard Testing Practices How to Solve namespace SharePointLogicTests { [TestClass] public class ClassWithDependenciesTests { [TestMethod] public void TestGetNodesByName() { // Arrange. INode node = new SharePointNode(); IDataRepository repository = (IDataRepository)MockRepository.Stub<IDataRepository>(); repository.Stub(x => x.Where).Returns(repository); repository.Stub(x => x.Select).Returns(node); repository.Replay(); // ...
  • 10. Standard Testing Practices How to Solve // Act. ClassWithDependencies testClass = new ClassWithDependencies(repository); INode testNode = testClass.GetNodeByName("Test"); // Assert. Assert.AreEqual(node, testNode); } } }
  • 11. Standard Testing Practices What about SharePoint? • Most of SharePoint object model has NO interfaces • Worse, most also are Sealed classes, meaning no extending and overriding the SharePoint behavior • Most SharePoint objects require active connection and instance of SharePoint on local server • Unlike database projects, resetting and recreating state in SharePoint is way more difficult
  • 12. Standard Testing Practices A SharePoint Common Problem public string GetNameOfSharePointWebSite(string url) { using (SPSite site = new SPSite(url)) { using (SPWeb web = site.OpenWeb()) { return web.Name; } } }
  • 13. Pex and Moles • What is it? • Pex discovers boundary conditions of your tests and automates test creation • Visual Studio Add In • Developed by Microsoft Research • Available for Academic or for MSDN subscribers
  • 14. Pex and Moles • Moles is, simply put, a mocking and stubbing framework • Different than other traditional mocking frameworks • Uses detours to custom delegates via runtime instrumentation
  • 15. Pex and Moles • Pex and Moles are not mutually exclusive • Neither are dependent upon one another
  • 17. Pex and Moles • As your dependency on SharePoint Object Model grows, more detours required for testing • In most cases, more tedious than beneficial • If only could have most of the basic SharePoint behaviors pre-generated • Solution: Microsoft.SharePoint.Behaviors
  • 18. DEMO SharePoint Mole Behaviors
  • 21. Observations • Even behaviors are not complete • A lot of mocking activity may get repeated
  • 22. Observations • Even behaviors are not complete • A lot of mocking activity may get repeated • Unit test setup logic gets refactored into common assemblies
  • 23. Observations • Even behaviors are not complete • A lot of mocking activity may get repeated • Unit test setup logic gets refactored into common assemblies • Save time: create scenarios
  • 24. Observations • Even behaviors are not complete • A lot of mocking activity may get repeated • Unit test setup logic gets refactored into common assemblies • Save time: create scenarios • Use BDD-style approach for testing
  • 26. Gotchas • More moles unit tests = much longer test execution time
  • 27. Gotchas • More moles unit tests = much longer test execution time • Continuous Integration
  • 28. Gotchas • More moles unit tests = much longer test execution time • Continuous Integration • This will take time to learn, research, and debug
  • 29. Gotchas • More moles unit tests = much longer test execution time • Continuous Integration • This will take time to learn, research, and debug • Pay attention when it stops adding value
  • 30. Gotchas • More moles unit tests = much longer test execution time • Continuous Integration • This will take time to learn, research, and debug • Pay attention when it stops adding value • The GAC
  • 31. Gotchas • More moles unit tests = much longer test execution time • Continuous Integration • This will take time to learn, research, and debug • Pay attention when it stops adding value • The GAC
  • 33. Recommendations • If you can do it, build a facade in front of SharePoint
  • 34. Recommendations • If you can do it, build a facade in front of SharePoint • Make sure you are producing consistent behaviors
  • 35. Recommendations • If you can do it, build a facade in front of SharePoint • Make sure you are producing consistent behaviors • If you don’t know what SharePoint does, disassemble it
  • 36. Q&A
  • 37. Thank You! http://slidesha.re/ http://spkr8.com/t/8140 git://github.com/neraath/testing-in-sp2010.git

Editor's Notes