Unit Testing inSharePoint 2010      Chris Weldon   Dallas TechFest 2011
Before We Beginhttp://slidesha.re/       http://spkr8.com/t/8140git://github.com/neraath/testing-in-sp2010.git
Your Guide: Chris Weldon•   Fightin’ Texas Aggie•   .Net and PHP Developer•   UNIX and Windows Sysadmin•   Sr. Consultant ...
Agile, Microsoft, Open Technologies, UXApplied Training, Coaching, MentoringCertified ConsultingRural SourcingRecruiting Se...
Assumptions• You develop software• You unit test• You know the SharePoint API (or can follow along)
Standard Testing Practices                        A Common Problemnamespace SharePointLogic{    public class ClassWithDepe...
public IList<SPUser> GetUsersInSiteCollection(string siteUrl) {  using (SPSite site = new SPSite(siteUrl) {    site.CatchA...
Standard Testing Practices                A Common Problem• What if no database locally?• Did you write test logic to rebu...
Standard Testing Practices                               How to Solvenamespace SharePointLogicTests{    [TestClass]    pub...
Standard Testing Practices                                 How to Solve            // Act.            ClassWithDependencie...
Standard Testing Practices              What about SharePoint?• Most of SharePoint object model has NO interfaces• Worse, ...
Standard Testing Practices          A SharePoint Common Problempublic string GetNameOfSharePointWebSite(string url){    us...
Pex and Moles• What is it? • Pex discovers boundary conditions of your tests    and automates test creation • Visual Studi...
Pex and Moles• Moles is, simply put, a mocking and stubbing  framework • Different than other traditional mocking    frame...
Pex and Moles• Pex and Moles are not mutually exclusive• Neither are dependent upon one another
DEMOSharePoint & Moles     Example
Pex and Moles• As your dependency on SharePoint Object Model  grows, more detours required for testing• In most cases, mor...
DEMOSharePoint 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 refa...
Observations• Even behaviors are not complete• A lot of mocking activity may get repeated• Unit test setup logic gets refa...
Observations• Even behaviors are not complete• A lot of mocking activity may get repeated• Unit test setup logic gets refa...
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, r...
Gotchas• More moles unit tests = much longer test execution  time• Continuous Integration• This will take time to learn, r...
Gotchas• More moles unit tests = much longer test execution  time• Continuous Integration• This will take time to learn, r...
Gotchas• More moles unit tests = much longer test execution  time• Continuous Integration• This will take time to learn, r...
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...
Q&A
Thank You!http://slidesha.re/       http://spkr8.com/t/8140git://github.com/neraath/testing-in-sp2010.git
Upcoming SlideShare
Loading in …5
×

Unit Testing in SharePoint 2010

2,398
-1

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
2,398
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
35
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Our problem? Dependency on objects we don&amp;#x2019;t control. \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Unit Testing in SharePoint 2010

    1. 1. Unit Testing inSharePoint 2010 Chris Weldon Dallas TechFest 2011
    2. 2. Before We Beginhttp://slidesha.re/ http://spkr8.com/t/8140git://github.com/neraath/testing-in-sp2010.git
    3. 3. Your Guide: Chris Weldon• Fightin’ Texas Aggie• .Net and PHP Developer• UNIX and Windows Sysadmin• Sr. Consultant at Improving Enterprises• chris@chrisweldon.net
    4. 4. Agile, Microsoft, Open Technologies, UXApplied Training, Coaching, MentoringCertified ConsultingRural SourcingRecruiting Services
    5. 5. Assumptions• You develop software• You unit test• You know the SharePoint API (or can follow along)
    6. 6. Standard Testing Practices A Common Problemnamespace 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. 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. 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. 9. Standard Testing Practices How to Solvenamespace 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. 10. Standard Testing Practices How to Solve // Act. ClassWithDependencies testClass = new ClassWithDependencies(repository); INode testNode = testClass.GetNodeByName("Test"); // Assert. Assert.AreEqual(node, testNode); } }}
    11. 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. 12. Standard Testing Practices A SharePoint Common Problempublic string GetNameOfSharePointWebSite(string url){ using (SPSite site = new SPSite(url)) { using (SPWeb web = site.OpenWeb()) { return web.Name; } }}
    13. 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. 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. 15. Pex and Moles• Pex and Moles are not mutually exclusive• Neither are dependent upon one another
    16. 16. DEMOSharePoint & Moles Example
    17. 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. 18. DEMOSharePoint Mole Behaviors
    19. 19. Observations
    20. 20. Observations• Even behaviors are not complete
    21. 21. Observations• Even behaviors are not complete• A lot of mocking activity may get repeated
    22. 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. 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. 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
    25. 25. Gotchas
    26. 26. Gotchas• More moles unit tests = much longer test execution time
    27. 27. Gotchas• More moles unit tests = much longer test execution time• Continuous Integration
    28. 28. Gotchas• More moles unit tests = much longer test execution time• Continuous Integration• This will take time to learn, research, and debug
    29. 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. 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. 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
    32. 32. Recommendations
    33. 33. Recommendations• If you can do it, build a facade in front of SharePoint
    34. 34. Recommendations• If you can do it, build a facade in front of SharePoint• Make sure you are producing consistent behaviors
    35. 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. 36. Q&A
    37. 37. Thank You!http://slidesha.re/ http://spkr8.com/t/8140git://github.com/neraath/testing-in-sp2010.git
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×