Skip to main content

Tips & Tricks to write quality code for Microsoft Dataverse

By Betim Beja ·

This article is a step by step guide to writing a plugins and a workflows project with unit tests and coverage, using opensource tools developed by AlbanianXrm.

This guide will use the AlbanianXrm Extensions which offer two project templates for Visual Studio 2017 and 2019. You can get it from Manage Extensions in Visual Studio or download a copy from here.

Visual Studio Manage Extensions dialog showing AlbanianXrm Extensions in the Marketplace search results

Start by creating a new project in Visual Studio using the Dynamics Plugin Template by AlbanianXrm.

Visual Studio Create a new project dialog listing the Dynamics Plugin and Workflow templates by AlbanianXrm
Configuring the AlbanianXrm.Plugins project from the Dynamics Plugin Template in the AlbanianXrm.Customizations solution

Let's add the Unit Test project right away so we can do Test Driven Development (TDD).

Adding an AlbanianXrm.Plugins.Tests class library project next to the plugins project in Solution Explorer

To test our plugins we need FakeXrmEasy.9, XUnit, XUnit.runner.visualstudio and a reference to our plugins project. We can create a plugin EnsureAccountHasPrimaryContact on the plugins project

EnsureAccountHasPrimaryContact.cs
using System;

namespace AlbanianXrm.Plugins
{
    public class EnsureAccountHasPrimaryContact : PluginBase
    {
        protected override void Execute(IContext context)
        {
            throw new NotImplementedException();
        }
    }
}

And a test class EnsureAccountHasPrimaryContactTests on the Plugin Tests project.

EnsureAccountHasPrimaryContactTests.cs
using FakeXrmEasy;
using Xunit;

namespace AlbanianXrm.Plugins.Tests
{
    public class EnsureAccountHasPrimaryContactTests
    {
        [Fact]
        public void PluginCanBeExecuted()
        {
            var context = new XrmFakedContext();

            var ex = Record.Exception(() => context.ExecutePluginWith<EnsureAccountHasPrimaryContact>());

            Assert.Null(ex);
        }
    }
}

Executing the tests at this point you get a failure because the implementation of EnsureAccountHasPrimaryContact throws a NotImplementedException. We can improve our plugin and the test in order to make it do what we need and have a green test.

Let's add our Coverage Project, this will be an empty project which references the test projects just for making sure there is a dependency and they will be built before we run our coverage report. We can use the MSBuild.AlbanianXrm.Coverage package to produce the coverage report with zero configuration.

NuGet Package Manager showing the MSBuild.AlbanianXrm.Coverage package for the AlbanianXrm.Coverage project

If we add the test project as a reference and build the solution we can find the coverage report in the output directory of our coverage project.

Coverage report for AlbanianXrm.Plugins with 100% line coverage of EnsureAccountHasPrimaryContact

We can repeat the above steps for our workflows. An example of a AddressGeocode workflow:

AddressGeocode.cs
using System.Activities;

namespace AlbanianXrm.Workflows
{
    public class AddressGeocode : WorkflowBase
    {
        public InArgument<string> Address { get; set; }

        public OutArgument<string> Coordinates { get; set; }

        protected override void Execute(IContext context)
        {
            context.TracingService.Trace(context.Arguments.GetValue(Address));

            context.Arguments.SetValue(Coordinates, "Home");
        }
    }
}

An example of a test for the AddressGeocode:

AddressGeocodeTests.cs
using FakeXrmEasy;
using System.Collections.Generic;
using Xunit;

namespace AlbanianXrm.Workflows.Tests
{
    public class AddressGeocodeTests
    {
        [Fact]
        public void WorkflowCanBeExecuted()
        {
            var context = new XrmFakedContext();
            var inputs = new Dictionary<string, object>
            {
                { "Address", "Home Address" }
            };
            IDictionary<string, object> outputs = null;

            var ex = Record.Exception(() => outputs = context.ExecuteCodeActivity<AddressGeocode>(inputs));

            Assert.Null(ex);
            Assert.NotNull(outputs);
            Assert.True(outputs.ContainsKey(nameof(AddressGeocode.Coordinates)));
            Assert.Equal("Home", outputs[nameof(AddressGeocode.Coordinates)]);
        }
    }
}

By adding the references of our tests projects to our Coverage project

Reference Manager for AlbanianXrm.Coverage with the plugins and workflows test projects checked

The end result is that we have a coverage report which covers both projects.

Combined coverage report for the AlbanianXrm.Plugins and AlbanianXrm.Workflows assemblies

We offer consultancy and trainings for Microsoft Dynamics Implementations with this kind of quality. If you are interested let's get in touch.