ARTICLES

Home  > Articles  >  Unit testing your Sharepoint deployment
Unit testing your Sharepoint deployment
by Yaroslav Pentsarskyy

Here is the problem: you're managing SharePoint project (intranet, extranet, public site); you have all of your custom components tested and working; how about the solution overall along with your custom components? Are your sites created, templates deployed, lists and libraries provisioned, resourced copied, features installed and activated?

Here is how
to perform your automatic tests.

Custom components like webparts, features, event handlers code has to be tested separately in their respective unit tests.

Here are some snippets when testing general site items from above:

1. Checks whether web application features like resources have been deployed and activated

[TestMethod]
        public void WebAppFeaturesExist()
        {
            using (SPSite rootSite = new SPSite(TestConfig.SharePointUrl))
            {
                // Resources deployed
                Assert.IsTrue(rootSite.WebApplication.Features.Exists(new Guid(”xxxxxxxxxx-xxxxxxxx-xxxxxxxxx-xxxxxxxxxx”)));
            }
        }

2. Check whether site template has been deployed and exists

[TestMethod]
        public void SiteTemplatesExist()
        {
            using (SPSite rootSite = new SPSite(TestConfig.SharePointUrl))
            {

                ////Application Site Template deployed
                //Assert.IsTrue(rootSite.GetWebTemplates(1033).Exists(”CustomIntranetTemplate#0″));
            }
        }

3. Have you created any libraries or lists that are required , maybe libraries that store images for landing pages, all sorts of lists, templates that lists are based on?

[TestMethod]
        public void LibrariesExist()
        {
            using (SPSite rootSite = new SPSite(TestConfig.SharePointUrl))
            {
                using (SPWeb rootWeb = rootSite.OpenWeb())
                {
                    //Custom library created
                    Assert.IsTrue(rootWeb.ListTemplates.Exists(”Custom Library Template”));
                    using (SPWeb web = rootSite.OpenWeb(”/”))
                    {
                        Assert.IsTrue(web.Lists.Exists(”Custom Library”));
                    }
                }
            }
        }

4. Content types deployed and event receivers activated on them?

[TestMethod]
        public void ContentTypesExist()
        {
            using (SPSite rootSite = new SPSite(TestConfig.SharePointUrl))
            {
                using (SPWeb rootWeb = rootSite.OpenWeb())
                {
                    Assert.IsTrue(rootWeb.ContentTypes.Exists(”IntranetMainPage”));

                    //Event Receivers deployed on those content types
                    Assert.IsTrue(rootWeb.ContentTypes["IntranetMainPage"].EventReceivers.Count > 0);
                    Assert.IsTrue(rootWeb.ContentTypes["IntranetMainPage"].EventReceivers[0].Name == “CustomEventReceiver”);
                }
            }
        }

5. Do you have any custom features on webs, have they been deployed and activated?

[TestMethod]
 public void FeaturesExist()
 {
  using (SPSite rootSite = new SPSite(TestConfig.SharePointUrl))
   {
  // Has home page feature been activated

        Assert.IsTrue(rootSite.RootWeb.Features.Exists(new Guid(”xxxxxxxxxx-xxxxxxxx-xxxxxxxxx-xxxxxxxxxx”)));
   }
 }
 

Hope this helps, drop me a note if you have any questions.

Yaroslav Pentsarskyy, MVP
Blog: www.sharemuch.com