Testing CRM Plug-ins

During the development of CRM plug-ins, a developer will test his/her plug-in before putting in production server to make sure it works as designed. One way to do it is to have a development environment to test plug-in. However, the turnaround time between deploy-test-debug-build-deploy is very significant even when putting the plug-in on disk.

It would be nice to test plug-in locally without deploying it on CRM server. The approach I am going to demonstrate is to execute a plug-in in a small EXE container. Since IPluginExecutionContext is just an interface, we can mimic the context in our test container. Effectively, you will be able to rapidly develop/test/debug plug-in without ever leaving Visual Studio until you are ready.


I created a sample solution containing a simple test context and a test plug-in on code.msdn (http://code.msdn.microsoft.com/MSCRMPluginDebugger). This sample shows you how to populate relevant input parameters and how to create CrmService behind context. You might need to add more implementation to TestContext to suite your needs but this sample should be a foundation you can build on for example pre/post image or MetadataService.

A developer can use this approach for both debugging during development or for writing unit test. It would enable ISV to write unit test and run it from test harness/framework such as one provided by TFS.

Even though mocking IPluginExecutionContext can emulate container that plug-in will execute in, it is only approximation. Other differences can still make running in production different from test context, for example:

  • Security context. CRM runs plug-in in service account (usually network service). Test contain runs plug-in in currently logged in user context. Files/registries may be accessible from log in user but service account may not , and vice versa.
  • HttpContext. Accessing HttpContext.Current is not recommended. If you need something that is available only HttpContext.Current, please let us know. Ideally, plug-in should be able to get everything from IPluginExecutionContext.

NOTE: Depending on currently logged in user, you may or may not be able to impersonate a particular user. Only AD user in PrivUserGroup can impersonate other user in CRM.