UPDATE: Fixed the video resolution issue. Higher resolution (720p) version is available.
- Integrate with MSTest so:
- I get as much of the TFS integration goodness I can get
- Gated check-ins
- Running tests on a build server
- Write and debug my code without having to constantly deploy to a CRM server to test and iterate on my code
- Mock things like Xrm.Page, web service calls, interaction with the HTML DOM, etc.
- Integrate with the Developer Toolkit
- Run tests OUTSIDE of the context of a browser
So I scoured the internet, tried a bunch of things I came across, and finally landed on approach which combines lessons learned from the following resources:
You can grab the finished example I show in the walkthrough here:
This is probably the most promised, but never published blog post on my list of potential posts. I’ve managed to convince enough CRM developers I’ve run into that they should get some test driven development with unit testing religion. Especially because of the added productivity benefits for CRM plugin development. This post isn’t to convince you of the benefits of unit testing. There are plenty of articles/blogs which cover that topic just fine. I’m not interested in debating the virtues of unit testing. Love it or leave it;).
The purpose of this post/video is to show you how I develop and unit test plugins. There are a few articles out there about how to unit test CRM 2011 plugins. I haven’t been satisfied with the approaches I’ve seen. I landed on an approach which combines using the CRM 2011 Plugin Testing Tools for mocking IServiceProvider and Moles fore detouring types used inside the plugin which are difficult or time consuming to mock such as OrganizationServiceContext, Web Service calls, and others.
You can grab the finished example here:
You can also grab the starter solution I mention in the video here:
Once you’ve learned the basics in the video, hop on over to the series below for some more advanced examples:
The series above focuses on using Fakes in VS2012. Fakes is the successor to Moles. However, the general techniques still apply whether using Fakes/Moles. If you are using VS2012, then use Fakes. If you are using VS2010, then use Moles.
I find many people who are new to unit testing have questions about where it fits in relation to other forms of testing. Unit testing does not replace other forms of testing such as integration testing (i.e. testing the plugin running in CRM). TDD through unit testing is an upfront developer discipline. It requires a certain culture and mindset on a dev team. In my opinion and experience, it helps me produce better code and allows me to catch more (not all) bugs early on, before handing things off to other testers. It also makes the others testers lives/jobs easier.
When applying this approach to CRM plugins, there’s a bonus side effect that (for me) allows me to be more productive in authoring my plugin code because I can:
- Iterate (make code changes, build, debug and/or test, repeat) rapidly without needing to deploy to a CRM server
- Iterate on my code rapidly without affecting the external systems
- Use code coverage to identify untested scenarios which enable me to build better, more complete tests.
- This, in turn, allows me to build better, less buggy code.
- The nature of having a suite of automated tests allows you to identify issues and regressions in your code early on as you make changes
- This becomes more and more valuable over time as you build out your plugins and corresponding tests
- Other devs don’t have to know how to test your code. They just run the tests you’ve written
As part of building my WP7 app, I spent a fair amount of time trying to get code coverage working for my unit tests. I tried a number of different approaches including using the Silverlight Unit Test Framework and NUnit + dotCover. I finally landed on an approach that worked for me using the unit testing capabilities in Visual Studio 2010. Since this is a much confused topic with very little info on the web on how to successfully get working for Silverlight/Windows Phone apps, I published a screencast on the subject. I hope you find it useful.
"Code coverage is a measure used in software testing. It describes the degree to which the source code of a program has been tested."
In this screencast, you will learn how to get code coverage for your Silverlight and/or Windows Phone ViewModels through unit testing. You can learn more about code coverage in Visual Studio 2010 from: