Applications Services Blog
Get the latest thought leadership and information about the role of Applications Services in an increasingly interconnected world at the HP Blog Hub.

Unit testing custom Microsoft Dynamics CRM code – Part 1

When writing custom code that interacts with Microsoft Dynamics CRM, it is important to:

1. Unit test your code

2. Use an automated unit testing framework to automate your unit tests

3. Write unit tests that do not depend on external resources such as a Microsoft Dynamics CRM application server or SQL Server

 

Over the course of my next several posts, I'll be showing you some best practices for unit testing Dynamics CRM-facing code using Visual Studio unit testing tools and the Moq object mocking library. But before we get to the how, I'd like to spend the rest of this post talking about why the points I mentioned above are important.

 

Why unit test?

Looking at a high-level software development lifecycle of analysis-design-build-test-deploy, it’s generally accepted that the later a defect is identified in the lifecycle, the more it costs to fix. Consider the following example. A developer who is working on a method that adds two numbers accidentally types “-“ instead of “+” in the code. If the developer catches the error before checking in the code, the cost incurred (in time and dollars) to fix it is negligible. On the other hand, if the defect slips through all the way to production, fixing it now requires not only changing the code, but also potentially fixing impacts to other components, rewriting documentation, redesigning training materials and redeploying the package, not to mention the loss of goodwill from the customer.

 

In addition to minimizing the cost of defect remediation, there are several other reasons to adopt unit testing. According to the Wikipedia entry on unit testing, there are five main benefits:

1. You will find defects earlier in your development cycle.

2. Unit tests make for easier refactoring and other code changes.

3. A comprehensive set of unit tests will make integration testing easier.

4. Unit tests can be thought of as "living documentation" for your code.

5. Unit tests, especially in a test-driven development methodology, are quasi-design documentation.

 

Why use an automated unit testing framework?

As the name suggests, an automated unit testing framework allows you to automate your unit tests. This lets developers quickly and easily run tests throughout the build process. By not requiring manual action to run unit tests, costs are also minimized. An automated testing framework is also necessary for continuous integration, but that's a topic for another time.

 

Why write unit tests that do not depend on external resources (Dynamics CRM application server, SQL Server, etc.)?

First, by definition, a unit test is a test of the smallest testable piece of an application. If your unit tests rely on accessing the CRM server, in addition to your code, you are actually testing network connectivity, CRM functionality, the database server and so on.

 

Second, trying to maintain an up-to-date Microsoft Dynamics CRM environment for unit testing is a pain. It's often hard enough to keep good data in a properly configured QA environment for integration testing and UAT, and it's even harder when you have to deal with multiple developers doing TDD maybe even before your actual QA environment is online.

 

To accomplish this goal, we need to use a mock object framework. Mock object frameworks allow unit tests to run your code against a mock CRM service so that the tests only validate that the code sends the right requests and correctly handles responses without requiring an actual CRM service.

 

How do you do this with Microsoft Dynamics CRM?

There are a number of unit testing tools for C# code, but in this series we'll exclusively use the unit testing tools that are available in Visual Studio 2012. Although I'll be using the built-in Visual Studio tools, you should be able to achieve the same results with alternative tools such as NUnit.

 

There are also multiple mock object frameworks for C#, but in this series we'll be using Moq. In the past, I've worked with Rhino Mocks, and while I still think it's an incredibly powerful tool, I think Moq is better for this series. Microsoft also offers a mock object framework called Microsoft Fakes that is available in the premium and ultimate editions of Visual Studio. So if you are using those, you may want to take a look at Fakes.

 

That's all for now. In my next post we'll get started testing. See you then!

 

syndication-footer.png

Leave a Comment

We encourage you to share your comments on this post. Comments are moderated and will be reviewed
and posted as promptly as possible during regular business hours

To ensure your comment is published, be sure to follow the community guidelines.

Be sure to enter a unique name. You can't reuse a name that's already in use.
Be sure to enter a unique email address. You can't reuse an email address that's already in use.
Type the characters you see in the picture above.Type the words you hear.
Search
About the Author
Microsoft Dynamics CRM solution architect, C# developer, MBA, husband, father, Auburn man. Follow me on Twitter @lucas_is.
About the Author(s)
  • Business Development leader for HP’s Testing Practice in Americas and TaaS Global Capability Leader
  • Anthony works in the ES Apps Enterprise APJ . He is a self-motivated and well-disposed individual. He is a certified SAP FICO consultant. He has over twenty years industry experience in business transformation and Information Technology consulting (SAP). With excellent, interpersonal and communication skills, Anthony is able to influence and motivate at all levels. An empathic team player, He derives fulfilment in contributing towards others' development. Anthony possesses good analytical and problem-solving skills. He is a quick thinker, remains calm under pressure, handles multiple tasks, and accomplishes objectives with excellent time management. He is flexible and accustomed to meeting high standards and deadlines. Anthony possesses practical knowledge of business processes and concepts and has a keen eye for detail. He is aware of the importance of an effective system and quality management.
  • Bryan Coapstick is the Director of Mobile Innovation at HP. In this role, he is responsible for driving strategic mobility initiatives and key industry partnerships to enable clients the ability to leverage the mobile channel to effectively reach their customers. Bryan’s expertise lies with several Fortune 200 companies in both public and private sectors that focus on business technology and emerging strategies. Bryan demonstrates innovative leadership though public speaking, panel discussions and blogging on the various ways mobility is changing our daily lives.
  • Benjamin Romberg has over twenty five years experience in the IT industry, the last twelve of which have been as a testing professional. He has experience as a test analyst, performance tester, automation specialist, test lead, test manager, test consultant and test executive. He has worked on all types of testing engagements ranging from small web based applications through to core system upgrades. Benjamin holds a Bachelor’s Degree in Genetics, a Post Graduate Degree in Software Engineering and an Executive Masters in Business Administration. Benjamin now leads HP’s Testing and Quality Assurance Practice in Asia Pacific and Japan.
  • Over the past 5 years, I have participated in the different stages of delivering Microsoft Enterprise Applications to customers as large as 40.000 concurrent users across Europe with a focus on the public and call center sector. During this period, I worked in numerous technical (lead) and business consultant roles. Using this experience, I am currently a certified Microsoft Dynamics CRM 2011 expert which specializes in guiding enterprise customers and implementation teams through the process of implementing Microsoft Dynamics CRM 2011.
  • Craig has been with HP for 10 years working on the solutioning side as a Solution Architect. Primarily works with Microsoft technologies and has for the better part of 16 years. This past year Craig has worked mostly with Windows AZURE and the mobility side of Windows Phone 7/8 and Android.
  • Master Business Consultant and Distinguished SE with 35+ years of IT experience, specializing in SOA and Integration.
  • Daniel Amor (EMEA, AMOD, Cloud, Portals, Web Apps and SOA domain expert): Daniel has designed, won, led and delivered large-scale, complex applications-related projects by acting as a trusted advisor to clients. Daniel conducts regular training and client-facing sessions, and he has authored six books and numerous articles in European and US-based magazines and newspapers on IT change. He is a regular lecturer and he speaks on Portals, E-Business and Cloud.
  • Global HP Microsoft Business Applications Practice. Microsoft Dynamics CRM subject matter expert. Based in Manchester, United Kingdom. @DarrenCRM on Twitter
  • Solutions Architect working at HP since 2002 with focus on Applications Modernization, located at Rio de Janeiro, Brazil
  • Managing Consulting specialising in Business Transformation and Value Management for Defence, Healthcare, Public Sector, Criminal Justice and Telecommunications.
  • Guido Arndt is since May 1997 business consultant and project manager @ Hewlett Packard Enterprise Services. Main focus is strategic and organizational alignment of logistics, sales and production processes in the automotive and manufacturing industry as well as business performance projects in finance and telecommunication industry. Guido has experience as change agent and transformation manager in people-driven merger & aquisition projects focusing government, consulting firms, manufacturing plants and consumer brands.
  • Horia Slușanschi serves as an Agile Coach leading the HP Agile Mentoring Office and the HP Software Engineering Profession. He is passionate about helping teams and leaders to find joy in work in the pursuit of customer delight.
  • CISSP, Open CA Master Architect Technical Consultant with 28+ years of IT experience specializing in Application Security, Solution Architecture, Portals, Social Business, and Integration
  • Jim has been with HP for 33 years and currently leads a global Strategic Capabilities Management team, with a specific focus on Business Analysis and Configuration Management. Jim also manages a team within the US State, Local, and Education division of HP. He was a member of the IIBA committee that created a Business Analyst Competency Model and he participated in the development of IEEE standards. Jim graduated from Harvard University, the University of British Columbia, and Ottawa Theological Hall. He currently lives in Toronto, Canada.
  • SAP Technologist from APPS GD SAP Practice - Manila covering SAP NetWeaver EP, SAP Mobility and general SAP solutions.
  • WW product marketing manager for HP Applications Modernization and Transformation Consulting Services. 35+ years of IT technology and services experience.
  • Laurence has over 35 years experience in the IT industry and has spent 27 years delivering knowledge management (and before that became fashionable, Artificial Intelligence and knowledge-based systems) transformation projects to clients in Government, Industry, Manufacturing and Telecomms industries. He has developed and implemented knowledge management (KM) strategies for HP's clients and for business areas in HP (for BPO and for our Consulting business) and believes that KM strategy is essential for an effective KM practice.
  • . . Service Systems . .
  • Lori Lewis leads Global Digital and Integrated Marketing for the Applications and Business Services division in HP’s Enterprise Services. The organization is focused on providing clients with enterprise applications services and solutions to solve some of their most pressing business problems. For over 2 decades, she has championed innovative communication approaches to drive and grow market share and build motivated, empowered teams. She is a compelling, passionate ambassador of the art and science of marketing and the amazing results it can achieve when done with integrity and passion. Ms. Lewis’ credentials, track record, and enthusiasm are essential ingredients her team relies on as they work together to establish HP as the undisputed leader in information management and analytics services.
  • Microsoft Dynamics CRM solution architect, C# developer, MBA, husband, father, Auburn man. Follow me on Twitter @lucas_is.
  • CRM enthusiast, avid techie
  • More than 10 years of experience in developing effective and innovative marketing and communication strategies. Recently I have studied the effects of social media and disruptive technology on business, marketing, and culture. Follow me on Twitter: @micaelaraimondi
  • Agile Champion and coach. Passion in software development using lean and agile techniques. Software is a people business!!
  • Have 11 years of experience in SAP Consulting, Solutioning and Account management.
  • I have over 20 years of I.T. experience predominantely in applications development. In addition to my I.T. experience I spent 7 years as the Director of Store Services at Canada's third largest grocery retailer.
  • Shafraz Nasser is the Lead Microsoft CRM Architect with HP Enterprise Services in the South Pacific Microsoft Dynamics Practice. His focus is on selling, architecting and overseeing delivery of Microsoft Dynamics CRM based implementations within New Zealand.
  • Shefali Dua is leading Worldwide BPaaS Strategy & Solution Innovation team at HP Enterprise Services as the Solution Innovation Manager. Her focus is currently assisting in planning and delivery of industry solutions for verticals like social media, big data, mobility and security on the HP Enterprise Cloud offerings for CRM Technologies.
  • Steve Petruno is a Product Manager for HP’s Global Oracle Services with over 24 years experience in the Information Technology industry, project Management, Human Resources, Payroll and Benefits, ERP implementations, upgrades, acquisitions and divestitures.
  • I have worked for HP as a business consultant for seven years, providing process, strategy and business transformation advice in the Financial Services and UK Government sectors. As part of this I have used Lean and Continuous Improvement techniques to assess and improve business performance, ensure regulatory compliance and improve environmental sustainability.
  • Tom is the Global Product Marketing lead for HP's Cloud Applications Services. Tom has over 20 years of experience in information technology and a proven track record for implementing innovative client solutions in several industries including government, healthcare, and telecommunications. He thrives on developing services and solutions which result in business process improvement through greater systems efficiency.
  • Vijay Seetharaman is an Enterprise Architect within HP Enterprise Services - Industry Transformation Consulting practice, focusing on Cloud and Mobility


Follow Us