Learn how to write automated unit and integration tests for Dynamics CRM to improve quality of CRM solutions. This include testing plug-ins, custom workflow activities and workflows.
IAC 2024 - IA Fast Track to Search Focused AI Solutions
Test & Dynamics CRM - extremeCRM Berlin 2012
1. For Microsoft Dynamics CRM Partners and Customers
Testing & Dynamics CRM
2011
Wael Hamze
CRM Architect
2. For Microsoft Dynamics CRM Partners and Customers
Agenda
• Scope
• Current Reality of Testing
• Objectives
• How?
• Future Considerations
• Summary, Next Steps & Conclusion
• Questions
3. For Microsoft Dynamics CRM Partners and Customers
Current Reality of Testing
• No Testing
• Manual Testing in the end
• It works for me
• Testing using one client only
• Testing using no real data
• Assume all data is correct
• Difficult to test Dynamics in general
4. For Microsoft Dynamics CRM Partners and Customers
Objectives
• Show how to easily test Plug-ins & Custom
Workflow Activities
• Provide best practices for testing Plug-ins
& Custom Workflow Activities
• Encourage testing practices in CRM
Projects
• Improve Quality & Reliability of CRM
Projects without impacting Timescales
6. For Microsoft Dynamics CRM Partners and Customers
Tools & Patterns
• Tools
– CRM Developer Toolkit
– Unit Test Framework (MSTest)
– Mocking Framework (MOQ)
• Patterns
– Dependency Injection
7. For Microsoft Dynamics CRM Partners and Customers
Planning
• Plan for Testing from the beginning
– Decide on Tools & Framework
• CRM Developer Toolkit
– Provide a Testable Architecture
– Write Testable Code
8. For Microsoft Dynamics CRM Partners and Customers
Component Design/Architecture
CRM SDK/Platform
Framework
Common
Plug-ins WF Activities
Testing
10. For Microsoft Dynamics CRM Partners and Customers
Common & Framework Deployment
Framework
Common
Plug-ins
Merged Plug-in
Framework
Common
Plug-ins
ILMerge CRMDeploy
13. For Microsoft Dynamics CRM Partners and Customers
Solution Design Benefits
• Reusable Logic (Framework & Common)
• No Code Duplication
• Framework & Common can be Tested in
Isolation
• CRM Developer Toolkit
– Speed Up Development
– Easy Deployment
– Consistency
14. For Microsoft Dynamics CRM Partners and Customers
Write Testable Code
• Use Interfaces
• Use Dependency Injection
• De-couple Business & Data Access logic
15. For Microsoft Dynamics CRM Partners and Customers
Unit Testing
Dependency 1
Component
Dependency 2
Unit Tests
Mock 1 Mock 2
16. For Microsoft Dynamics CRM Partners and Customers
Why Write Unit Tests?
• Ability to Test Plug-ins & Custom Workflow
Activities without CRM Environment
• Ability to Test without Deployment
• Tests run much quicker
• Detect bugs very early and fix quickly
• No Remote Debugging means Easier
Development by Debugging in Visual
Studio
17. For Microsoft Dynamics CRM Partners and Customers
Testable Code
Dependency Injection
ClassA ServiceA
Uses
Builder
Container
ServiceA
Creates
IServiceAClassA
Uses
Implements
Create ClassA + Inject IServiceA
18. For Microsoft Dynamics CRM Partners and Customers
Dependency Injection
• Types of Injection
– Constructor Injection
– Property Injection
– Method Injection
• Execute (IServiceProvider)
• Resolves Dependencies using Factory or
Container
– Plugins & Custom Workflow Activities
19. For Microsoft Dynamics CRM Partners and Customers
XrmContainer
• Light Weight Dependency Injection
Container
• Runs inside the Sandbox
• Resolves Components based on Interfaces
– By Conventions
– By Registering Dependencies
• Use in Plug-ins & Custom Workflows
• Comes with the Framework
21. For Microsoft Dynamics CRM Partners and Customers
Code Design Benefits
• Mock Components behind Interfaces
– Dependencies
– Data Access Layer
• Unit Testing of Components in Isolation
– Testing with Mocked Dependencies
• Easily Resolve Dependencies using the
XrmContainer
22. For Microsoft Dynamics CRM Partners and Customers
Mocking With Moq
• Create a Mock Implementation of a
component using the Interface
• Setup the behavior of the Methods or
Properties you want to Mock
• Invoke the Methods or Properties on the
Mocked Object
• Verify that your Methods were called on
the Mocked Object
26. For Microsoft Dynamics CRM Partners and Customers
Unit Test Mock<IServiceProvider> Plugin
Create
Setup Mocks
Create
Execute
Call
Verify
27. For Microsoft Dynamics CRM Partners and Customers
Steps
• Create a Unit Test
• Create a Mock for the
IPluginExecutionContext
• Setup Mock Implementations for the Methods
and Properties you will call
• Call the Execute Method with Mocked Object
• Examine the Mocked Object or Catch any
Exceptions
• Perform your Asserts
37. For Microsoft Dynamics CRM Partners and Customers
Unit Test Mock<Extensions> Workflow Runtime
Create
Setup Mocks
Create + Inputs
Create + Inject Activity + Extensions
Execute
Workflow Activity
Invoke
Call
Verify
Outputs
38. For Microsoft Dynamics CRM Partners and Customers
Steps
• Create a Unit Test
• Mock any Workflow Services extracted from
CodeActivityContext (ITracingService,
IWorkflowContext,
IOrganizationServiceFactory)
• Create a Sandbox Workflow Runtime using
the Mocked Services to Test the activity
• Verify the Mocked Objects, Catch any
Exceptions & Check Activity Outputs
• Perform your Asserts
45. For Microsoft Dynamics CRM Partners and Customers
Unit Testing Tips
• Write as less code as possible
– Move common code to Common Library
– Test common code in Common Library
• Mock Logic in Common Library
– Logic already tested
– Focus testing on the plug-in
• Use the Unit Test Base Classes
– Speed Up your testing
– Make it easier
• Mock only methods you need in your test
46. For Microsoft Dynamics CRM Partners and Customers
Integration Testing
Dependency 1
Component
Dependency 2
Integration Tests
Dependency 3
S
c
o
p
e
47. For Microsoft Dynamics CRM Partners and Customers
Why Write Integration Tests?
• Make sure Plug-ins & Custom Workflow
Activities work within the CRM
Environment
• Make sure Deployment process works
• Create Automated Tests that be re-run to
detect Integration Issues between
different components in the same solution
or other solutions
48. For Microsoft Dynamics CRM Partners and Customers
Plug-insIntegration
Test
Organization
Service
Pre-Validate
Plug-in
Pre-Operation
Plug-in
Platform
Post-
Operation
Plug-in
Asynchronous
Plug-inDB
49. For Microsoft Dynamics CRM Partners and Customers
Steps
• Make sure all Unit Tests Pass
• Deploy Plug-in to Target CRM Environment
• Write Integration Tests that will test the
functionality of plug-ins by Invoking the CRM
Platform Services
• Query CRM or Check Return Values to Assert
• If tests fail look at diagnostics before
attempting any Remote Debugging
• Use Plug-in Profiler if needed
53. For Microsoft Dynamics CRM Partners and Customers
Custom Workflow Activities
Integration
Test
Integration Test Workflow
Inputs
Workflow
Activity
Assert
Pass Fail
Trigger
Check Status
54. For Microsoft Dynamics CRM Partners and Customers
Steps
• Make sure all Unit Tests Pass
• Deploy Activity to Target CRM Environment
• Create a Test Workflow Integration Test in Dynamics
– Add Custom Workflow Activity
– Set Input Parameters
– Add steps to verify output parameters
• Create an Integration Test in your Visual Studio
Integration Project
– Create a test entity
– Invoke the Workflow on the entity
– Wait until Workflow Pass or Fails
– Report Feedback to Integration Test
60. For Microsoft Dynamics CRM Partners and Customers
Integration Testing Tips
• Use Base Classes for your tests
• Think about clean ups before or after
Tests
• Impersonate the user with the correct
privileges
• Run your Integration Tests on your DEV,
SIT & UAT environments
• Run your Integration Tests often
62. For Microsoft Dynamics CRM Partners and Customers
Future Considerations
• JavaScript Testing
• End to End Tests
– Coded UI Tests
• Load & Performance Testing
• Testing from different geographical locations
• Continuous Integration
• Test Driven Development
• Multi-Browser Testing
63. For Microsoft Dynamics CRM Partners and Customers
Summary
• Visual Studio Solution Design
– CRM Developer Toolkit
– Framework & Common Libraries
– ILMerge for Plug-ins & Workflow Assemblies
• Code Design
– Interfaces & Dependency Injection
– XrmContainer
• XRM Testing Framework
– Unit & Integration Tests
64. For Microsoft Dynamics CRM Partners and Customers
Next Steps
• XRM Developer Framework
– Built on top of the CRM Developer Toolkit
– Productivity Tools for XRM Developers
– Code Generation Extensions
– Silverlight Framework
– Testing Framework
• Coming to CodePlex Soon !!!
– http://xrmdevframework.codeplex.com/
65. For Microsoft Dynamics CRM Partners and Customers
Conclusion
• Plan for Testing from Beginning
• Test as Early as Possible
– Increased Confidence
– Detect Bugs Early -> Easier to Fix
– Improve Quality
– Happy Users
• Automate your Testing
• Use XRM Testing Framework to Improve
Productivity
67. For Microsoft Dynamics CRM Partners and Customers
Wael Hamze
Thank You to all of our Sponsors
And Thank YOU for Attending
eXtremeCRM 2012 Berlin!
Please remember to fill out your session evaluation form.