Home > MS CRM 4.0 > Plug-in Unit Test

Plug-in Unit Test


I came across a good article about Plug-in Unit test , find below the info. Thanks to Akezyt Janedittakarn for his effort

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.

PLug-IN

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.

Advertisements
Categories: MS CRM 4.0
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: