Why CRM Developers should unit test their code

I haven’t written any unit tests for quite some time but I have decided to get back on the testing band wagon.  I will be writing more blog posts on unit testing but I thought I would start with a blog post about the benefits of unit testing.

When you mention unit tests to CRM developers or any developers is a bit of Marmite question.  Some developers love unit tests and some developers dislike writing unit tests and there isn’t much in the middle.

I believe most developers know they are beneficial but because they involve the tricky/boring setting up testing data and if you don’t write tests regularly they can seem difficult to write.

Writing Unit tests will mean you have more code to maintain but the benefits greatly out weight the negatives.

My experiences of unit testing and CRM projects is a lot of CRM practices are not using unit tests (I said this is my experience) or some of them write a few tests but this is usually down to the individual developer rather than a company process.

Why don’t CRM Developers unit test

Here are some of the common reasons/excuses

  • It takes too long
  • I haven’t got time
  • Integration tests will test the code
  • It’s too difficult to test CRM code because of PluginContext
  • The company doesn’t make unit testing compulsory

Benefits of Unit Testing

  1. You already test your code, make the tests reusable and available to others
  2. Tests prove the code works
  3. Unit Tests make you try to break and then fix your code
  4. You can make big changes to the code and test the code still works
  5. The tests can be run every time the code is changed
  6. Testing is not slower
  7. Writing code to be tested creates better code
  8. Testing reduces the number of bugs in the code
  9. Refactoring the code can be done easier and with confidence
  10. It’s great when you get a green light
  11. Unit tests make the code more readable and understandable
  12. Testing before you code helps improve the design of the code
  13. Writing unit tests stops coders block
  14. Less bugs make it out of Development
  15. Bugs unit tested stay fixed
  16. Unit Tests will help you when you have forgotten the business logic
  17. Changing code in one part of the system can break code in another part of the system
  18. You only write test for the required functionality

You already test your code, make the tests reusable and available to others

When a CRM developer writes some code he often tests the code either by creating a console app or by debugging the code (either using the plugin profiler or remote debugging on the server).

I know you are testing your code because you have to test the code before you deploy it.  A lot of CRM developers hope they will find the bugs by integration testing and testing the code from the front end (the CRM web application).

The problem with integration testing is it takes longer to test the code and you have to create the data and test the functionality again every time.

Once you write a unit test to test one part of your functionality you can rerun the test again and again, instead of having to run through the process manually.

Instead of writing a console app or debugging the code, unit tests will enable you have tests you can rerun and more importantly you will have tests other developers can read and run.  If in the future another developer has to fix some code in that area they will have tests to help document the code and will be able to make changes and test the code.

Tests prove the code works

Writing and running unit tests shows the code works and passes the tests.  This depends on you writing effective tests but most of the time you will write tests to test the functionality required.  If you are using Test Driven Development you will write the tests first and then write the code.

Unit Tests make you try to break and then fix your code

When writing unit tests developers will often try to break the code by passing in Null values or unexpected inputs.  These are easy tests to write and help reduce easy/lazy errors from entering the production system.

Often if you are not unit testing the code you don’t think to write defensive code because the main priority is to test the functionality required

You can make big changes to the code and test the code still works

One of the main benefits of writing unit tests is you have created tests to test the functionality.  This means you can change the code and run the tests again to see if it still works.  You can refactor the code with confidence you can test it.

This is an important point for developers who are scared changing some legacy code because they are not sure what code depends on what.

The tests can be run every time the code is changed

Code can get changed lots of time particularly if it’s in a core piece of functionality.  Having tests which can be easily and quickly run every time the code is changed will give you confidence nothing is broken.

When bug fixing often CRM Developers will fix parts of the code they didn’t work on or have little knowledge of.  Unit tests will give the CRM developer confidence their fixes haven’t accidentally broken the code.

Testing is not slower

It would seem logical to think writing units would make the process of writing code longer.  The truth is this isn’t true.  A lot of the time spent on code is fixing bugs after the initial development is done.  Unit testing will help you find more of these bugs up front and reduce the time spent bug fixing.

Writing code to be tested creates better code

When you write code which is easily testable it tends to be better designed and adheres to the single responsibility principle and the code is more modular and reusable.

The developer will also spend more time thinking and designing the code.  Code which is easy to test is easier to maintain and has loose coupling and dependency.

Testing reduces the number of bugs in the code

When a developer writes unit tests they test their code in a more systematic approach and testing various different scenarios.  When CRM Developers manually test their code they usually go follow one or two successful paths through the code.

Refactoring the code can be done easier and with confidence

Refactoring the code can be a lot more confidence when the developer can test the code with unit tests.

It’s great when you get a green light

It’s a great to write a test and then get the green light.  Not only is it a small win it’s great to see how many tests you have running against a piece of code.

Unit tests make the code more readable and understandable

Unit tests

Testing before you code helps improve the design of the code

Writing tests before you have written the code will help the developer think about the design of the code, what they are trying to achieve.

When thinking about writing tests for code the developer will try to simplify the code to make it easier to test.   This process will make the code easier to maintain, debug, extend and understand.

Writing unit tests stops coders block

Writing unit tests stops developers suffering from coders block.  Coders block is when a developers gets overwhelmed by the task of designing the code and doesn’t know where to start.  Writing units tests gets the developer writing code and gives the developer an easy place to start.

The developer can refactor and improve the design of the code later and will be able to test the code works with the unit tests.

Less bugs make it out of Development

CRM Development is complex and the code can contain many bugs.  The aim is to squash as many in the development environment as possible.  Unit testing is one of the tools used to find and fix bugs.

Code reviews and integration testing are the other two main bug detectors.

Unit testing won’t find all the bugs and doesn’t mean you don’t have to do code reviews and integration testing but it will help the CRM developer to find some bugs in his code.

Finding bugs in the development environment is quicker than finding bugs in a production environment, logging the bug, recreating the bug, debugging the code, fixing the bug, retesting and pushing the bug all the way back up towards production.

Bugs unit tested stay fixed

When you fix a bug and unit test the fix, it’s more likely to stay fixed and if it isn’t fixed it can quickly be tested again.

Unit Tests will help you when you have forgotten the business logic

Unit tests are a great way to document the code because you it’s easy to see what the code should be doing by looking at what the test is testing.

Business logic fades (quicker than you think) so a developer has a bug to fix the unit tests will help the developer understand what the code should do.  The CRM developer will be able to quickly test the code with the unit tests.

Changing code in one part of the system can break code in another part of the system

I call this “accidental bugging,” and
it seems to occur a lot near the end of a software project, when developers
are under pressure to fix existing bugs. Sometimes they introduce
new bugs inadvertently as they solve the old ones. Wouldn’t it
be great to know that you broke something within three minutes of
breaking it? We’ll see how that can be done later in this book.

You only write test for the required functionality

You only write unit tests to test for functionality required.  This is a good way to stop developers adding in extra functionality which might be needed in the future.

More reading on why you should unit test

Is Unit Testing worth the effort? 

I Pity The Fool Who Doesn’t Write Unit Tests

Top 12 Reasons to Write Unit Tests

Write Maintainable Unit Tests That Will Save You Time And Tears

Advertisements

17 thoughts on “Why CRM Developers should unit test their code

  1. aansari March 31, 2015 / 12:53 pm

    The most important thing is you can break or fix the code by testing the code, it can reduce the bugs of code, makes the code more understandable.
    The benefits given here are handy to consider when it comes to understand the use of unit testing.

    Like

  2. codebubb March 17, 2016 / 9:27 am

    Great point about unit testing being slower. This is one of the main things that always puts me off but it makes sense to spend more time writing tests and then less time debugging. The last piece of code I wrote was a nightmare for figuring out why it wasn’t performing as expected – unit tests probably would have prevented this!

    Like

    • Hosk March 17, 2016 / 9:39 am

      Writing unit tests seems slower but I have found the code is usually better designed with less dependencies,
      A unit tests document what the code does
      It’s easy to debug the code through the unit test
      you catch more bugs because you think about testing the code.
      The biggest benefit is you can refactor the code and test if it still works.

      It leaves a gap to test the queries but these are easily fixed and remain static once written

      Like

      • codebubb March 17, 2016 / 9:52 am

        All good points!

        Thanks for the great article 🙂

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s