How to write Unit Test Case for your Plugins using C#.

Hello Guys,

In our previous blog we have seen one of the important feature of Power Automate Flow which is Geofencing. Today we will see how we can write and test unit test case for plugin.



Do follow us to know more about Dynamics and Power Platform. Also if you like our blog then please comment and share this blog with your friends.

As being a CRM developer we have written so many plugins related to our project. There are some situation where we need to test whether our plugin is executing and running properly. Sometimes we need to write unit test cases for our plugin where we are providing inputs to plugin to check whether our plugin is satisfying all the conditions and functionality is achieved or not. 

So let's see how we can write test cases for plugin. 

For time being I have created one plugin named CustomerNumbering which is used for creating customer's unique number based on BU whenever a new customer record is created by user.




So now we will be writing Unit Test cases for this plugin. First of all we will be creating a new Unit Test Project in same solution.



As you can see we have created one Unit Test Project named DynamicsPlugins.UnitTest. Right click on the project and select Manage NuGet Package and select FakeXrmEasy.9. Install the package. 





Now add a reference of Dynamics.Plugins project.



Now right click on the project and add a new class file as shown below:   



As you can see we have one test class. Now we will be creating Test Method for various test cases but before that we need to decide what scenario should be there for test cases.

If you see our plugin, inside the execute method we have first variable which is Context. So here context will be our first fake input which we need to create. Also we have next important variable which is service so we will be creating service instance using fake Xrm context. 



Keep in mind, here FakeService is not same as CRM's organization service.Now if you see the plugin code, we are checking that context should contains a input parameters of type Target Entity. So we will be creating one Target Entity whose schema name should be "blg_customer".



Now if we see our plugin then we have Message, Stage and User Id for Context which is Plugin execution context. 



Now we will be creating one dummy record for numbering scheme so that we can retrieve that record and based on that we can create a next auto number for the entity. Once dummy record is created, we will execute the plugin and check whether next generated number is correct or not.



So now our Unit Test case is completed. Now build the project and right click on Test file and run the test.



Now when we run the test, our test case got failed. So we need to fixed this also. Now if you see our plugin, we have used one query expression where we are passing BU id and due to that our query is not returning any record. Let's add this property also in NumberingScheme dummy record.



Now we will run the test again and check the output.



As you can see it got ran successfully.

Hope it helps...

 


   

Comments

Popular posts from this blog

Read Only Sub Grid & Editable Sub grid in Dynamics 365

Using Pipelines in Power platform for deploying solutions to environments.

Understanding Sales Process in D365.