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

PPT
Building Quality with Foundations of Mud
PPT
Automated Testing with Databases
PPTX
Quickly Testing Legacy C++ Code with Approval Tests
PDF
Test Dependencies and the Future of Build Acceleration
KEY
33rd degree
PPTX
Improving the Quality of Existing Software - DevIntersection April 2016
PPTX
JavaLand - Integration Testing How-to
PPTX
Riga Dev Day - Automated Android Continuous Integration
Building Quality with Foundations of Mud
Automated Testing with Databases
Quickly Testing Legacy C++ Code with Approval Tests
Test Dependencies and the Future of Build Acceleration
33rd degree
Improving the Quality of Existing Software - DevIntersection April 2016
JavaLand - Integration Testing How-to
Riga Dev Day - Automated Android Continuous Integration

What's hot (20)

PDF
Introduction to jest
PPTX
2014 Joker - Integration Testing from the Trenches
PPTX
Quickly testing legacy code
PDF
How to Un-Flake Flaky Tests - A New Hire's Toolkit
PDF
Adding unit tests with tSQLt to the database deployment pipeline
PPTX
Breaking Dependencies to Allow Unit Testing
PPTX
Mini training - Moving to xUnit.net
PPTX
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
PPTX
Improving the Quality of Existing Software
PDF
How To Use Selenium Successfully
PPTX
Quickly Testing Legacy Code - ACCU York April 2019
PDF
Mini-Training: NFluent
PPTX
Automated Software Testing
ODP
Testing JSF with Arquillian and Selenium
PDF
DSR Testing (Part 1)
PDF
Faking Hell
PDF
Functional tests for dummies
PDF
DSR Microservices (Day 1, Part 2)
PDF
Implementing Quality on a Java Project
PDF
DSR Testing (Part 2)
Introduction to jest
2014 Joker - Integration Testing from the Trenches
Quickly testing legacy code
How to Un-Flake Flaky Tests - A New Hire's Toolkit
Adding unit tests with tSQLt to the database deployment pipeline
Breaking Dependencies to Allow Unit Testing
Mini training - Moving to xUnit.net
Breaking Dependencies to Allow Unit Testing - DevIntersection Spring 2016
Improving the Quality of Existing Software
How To Use Selenium Successfully
Quickly Testing Legacy Code - ACCU York April 2019
Mini-Training: NFluent
Automated Software Testing
Testing JSF with Arquillian and Selenium
DSR Testing (Part 1)
Faking Hell
Functional tests for dummies
DSR Microservices (Day 1, Part 2)
Implementing Quality on a Java Project
DSR Testing (Part 2)
Ad

Viewers also liked (11)

PPTX
Unit Testing SharePoint Applications
PPTX
Managesp 160805190411
PPTX
Risk Management in SharePoint Governance
PPTX
V Greavu - Testing with Sharepoint
PPTX
Case Study for a SharePoint SDLC
PPT
Agile SharePoint Development With Scrum
PDF
Information architecture and SharePoint
PPT
An SDLC for SharePoint
PPTX
Testing SharePoint solutions overview
PDF
Quality Assurance in SDLC
PPTX
Deep Dive into SharePoint Topologies and Server Architecture for SharePoint 2013
Unit Testing SharePoint Applications
Managesp 160805190411
Risk Management in SharePoint Governance
V Greavu - Testing with Sharepoint
Case Study for a SharePoint SDLC
Agile SharePoint Development With Scrum
Information architecture and SharePoint
An SDLC for SharePoint
Testing SharePoint solutions overview
Quality Assurance in SDLC
Deep Dive into SharePoint Topologies and Server Architecture for SharePoint 2013
Ad

Similar to Unit Testing in SharePoint 2010 (20)

PPTX
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
PDF
Breaking Dependencies to Allow Unit Testing
PDF
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
PPTX
Test in action – week 1
PPTX
Mcknight well built extensions
PPTX
VT.NET 20160411: An Intro to Test Driven Development (TDD)
PDF
Performance Test Driven Development with Oracle Coherence
PPTX
Continuous Delivery - Automate & Build Better Software with Travis CI
KEY
Developer testing 101: Become a Testing Fanatic
PDF
Testcontainers - Geekout EE 2017 presentation
PDF
How to use selenium successfully
PPTX
Testing ASP.NET - Progressive.NET
PPTX
Easy Java Integration Testing with Testcontainers​
PDF
20111110 how puppet-fits_into_your_existing_infrastructure_and_change_managem...
PDF
The Many Ways to Test Your React App
PPTX
Automated php unit testing in drupal 8
PPTX
Introduction to SoapUI day 4-5
PDF
Advanced Java Testing
PPT
Getting Started with Test-Driven Development at Midwest PHP 2021
PDF
We Are All Testers Now: The Testing Pyramid and Front-End Development
More on Fitnesse and Continuous Integration (Silicon Valley code camp 2012)
Breaking Dependencies to Allow Unit Testing
Breaking Dependencies To Allow Unit Testing - Steve Smith | FalafelCON 2014
Test in action – week 1
Mcknight well built extensions
VT.NET 20160411: An Intro to Test Driven Development (TDD)
Performance Test Driven Development with Oracle Coherence
Continuous Delivery - Automate & Build Better Software with Travis CI
Developer testing 101: Become a Testing Fanatic
Testcontainers - Geekout EE 2017 presentation
How to use selenium successfully
Testing ASP.NET - Progressive.NET
Easy Java Integration Testing with Testcontainers​
20111110 how puppet-fits_into_your_existing_infrastructure_and_change_managem...
The Many Ways to Test Your React App
Automated php unit testing in drupal 8
Introduction to SoapUI day 4-5
Advanced Java Testing
Getting Started with Test-Driven Development at Midwest PHP 2021
We Are All Testers Now: The Testing Pyramid and Front-End Development

More from Chris Weldon (7)

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

Recently uploaded (20)

PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Cloud computing and distributed systems.
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
cuic standard and advanced reporting.pdf
PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
A Presentation on Artificial Intelligence
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Machine Learning_overview_presentation.pptx
PPTX
Spectroscopy.pptx food analysis technology
PPTX
MYSQL Presentation for SQL database connectivity
PDF
gpt5_lecture_notes_comprehensive_20250812015547.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
Cloud computing and distributed systems.
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
cuic standard and advanced reporting.pdf
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
A Presentation on Artificial Intelligence
Reach Out and Touch Someone: Haptics and Empathic Computing
Machine Learning_overview_presentation.pptx
Spectroscopy.pptx food analysis technology
MYSQL Presentation for SQL database connectivity
gpt5_lecture_notes_comprehensive_20250812015547.pdf
Network Security Unit 5.pdf for BCA BBA.
Digital-Transformation-Roadmap-for-Companies.pptx
Mobile App Security Testing_ A Comprehensive Guide.pdf
Review of recent advances in non-invasive hemoglobin estimation
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Building Integrated photovoltaic BIPV_UPV.pdf

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