Monday, July 30, 2012

Continuous integration with SharePoint (and automated unit tests)

I've been working a lot with SharePoint lately and as with any project, we add it to our Cruise Control.NET server. The first challenge was that we had to install SharePoint on the CC.NET server. It was not really a challenge, but more of a time-consuming task.


The second challenge was setting up automated unit tests. In our case we use WCF services that receive an SPToken (actually, the string representation of the SPToken). This token expires every now and then, and we had to come up with a way to get a token in order to connect to a WCF service. The solution was creating a method in a service that creates a token and sends it back. It's really not the best practice, and it's not something you want to deploy to production, but it works fine for a continuous integration environment. These are the steps we followed:


1) Create a new service that is used only in the CC.NET sandbox
2) Create a method in that service that returns a token. The method should run with elevated privileges and return a token of a hardcoded user account (preferably an administrator account)
3) Reference this service from the project that contains the unit tests (we use XUnit)
4) Instantiate a service client from the test case (something like this)



  XUnitServiceClient client = new XUnitServiceClient("BasicHttpBinding_IXUnitService");
  client.Endpoint.Address = new System.ServiceModel.EndpointAddress("http://mypc/_vti_bin/XUnitService.svc/soap");
  string result = client.GetToken();



5) Then just call the method you're trying to test and pass the token.

No comments: