Salesforce.com: To Unit Test or Not to Unit Test
Any Apex developer worth his or her salt knows the requirements for deploying Apex code to Production: at least 75% of the Apex code in Production must be covered, and every trigger must be touched. These are the minimum requirements. But are you satisfied with meeting the bare minimum requirements? Are there times when unit testing is a good idea, even if those test go over and above the minimum?
Consider this scenario. You’re a Salesforce administrator and developer, working in IT. You’ve been asked to develop a complex process to allow Sales Management to review and approve certain types of Opportunities. The project involves validation rules, complicated workflow rules, a thorny approval process, and absolutely no Apex. A week later, you’ve finished the job in the sandbox, and you’re ready to have it tested before you deploy to Production?
Should you write unit tests?
Technically, of course, you don’t have to unit test this process. There’s no Apex involved, so when you deploy to Production, any existing unit tests won’t even be run. The 75% rule doesn’t even apply. So why would you write unit tests? There are several reasons.
- First, unit tests are a great way to verify that your validation rules work properly. Do your validation rules catch every possible data entry error? Do they allow every possible combination of valid data to be entered? Testing this manually is tedious, and if you make one little change to a validation rule, you may forget to test all the ramifications of that change. Unit tests allow you to automate that testing.
- Second, unit tests are a great way to ensure that most your workflow works properly. Does the rule fire when you expect it to? Does your workflow field update work properly? Unit tests can’t verify everything about workflow (like whether email alerts or outbound messages are sent properly), but they help you verify at least part of your workflow; manual testing can verify the rest.
- Third, unit tests are a great way to test the approval process. When a record is submitted for approval, does it show up on the right people’s queues? When a record is approved or rejected, is the record updated properly? Unit tests are a great way to verify the flow of your approval process.
- Fourth, unit tests are a great way to test the end-to-end process while running as users with different profiles. As System Administrators, we usually test under our own Salesforce login, but often neglect to test as a non-admin user. With unit tests (using the System.runAs() method), we can write code that tests the process while running as users with a variety of profiles. Doing this testing manually can be tedious; with Apex, you can automate the tedium.
- Fifth, unit tests are a great way to ensure that, when the process that works fine in the sandbox is deployed to Production, it still works properly. How many times have you deployed to Production, but forgotten to activate workflow, or forgotten to deploy an important validation rule? Unit tests are a great way to ensure that what works in the sandbox also works in Production.
With apologies to William Shakespeare:
To unit test, or not to unit test: that is the question:
Whether ‘tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles,
And by unit testing, end them?