Sunday, 17 November 2013

Best Practices for writing Apex Test classes in Salesforce

15:26


Here are some best practices that I felt need to be followed while writing test classes in Salesforce , Do add your thoughts too



  1. All test methods should reside in a separate class from the class in which the method being tested resides.
  2. These classes should be appended with the word Test followed by the name of the class being tested, e.g. OpportunityServicesTest.
  3. These classes should all use the @isTest annotation.
  4. Each method in the production class should have, at a minimum, one corresponding test method in its test class and should be appended by “test”
  5. There should be a minimum of “Null Pointer Exception test” as part of negative testing for each method, specially the methods that accept parameters.
  6. A method without an assert statement is not considered a test method. Large number of relevant assert statements increases confidence in the correct behaviour of business logic.
  7. There should be a comment with each assert statement explaining what is being tested and what the expected output is
  8. Only use isTest(SeeAllData = true) on class methods in exceptional cases where there are sObjects that doesn't allow DML operation e.g. PriceBook creation
  9. No hard coded ids of any sObject in any test method.
  10. If a Constant needs to be asserted ,its a best practice to reference that from the Constant class or from Custom Labels or Custom Settings. Using hard coded string in unit tests( or any class for that matter) will trigger failures when things like Picklist values change
  11. All test data creation should be done from a Utility class. This allows for a streamlined creation of test objects that adhere to all the validation rules.
  12. Any business logic that needs to be tested should be enveloped within a Test.runAs(user) statement so profile restrictions can be tested. . Using any admin profiles should be avoided.
  13. All private methods should also have its corresponding unit test method. In the production code, add a public method for each private method and prepend it by “exposeForUnitTest_”.
  14. Creating multiple test method for testing that same production code method should be avoided. We want to ensure that our unit test methods are properly testing the logic but the same time the efficiency of the unit test method should not be ignored. All the unit test methods run with every deployment so the cumulative run time should be as small as possible
  15. Any asynchronous method testing should include Test.startTest and Test.stopTest. Test.stopTest forces the asynchronous methods to run so the results could be asserted.
  16. Any exceptions that are caught in the production methods should be tested by feeding the test data that throws exception. Exception Type and error message should be asserted.
  17. Every class should have test coverage close to 95% as possible. The focus should be on asserting method behaviour rather than increasing coverage. There are very few instances where a method behaviour is not straightforward to reproduce and hence test. These should be properly commented.
Resources :
http://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm (test assertions and debug/logging)
http://blogs.developerforce.com/engineering/2013/04/apex-test-code-segregation.html
http://www.laceysnr.com/2013/04/three-reasons-you-should-write-proper.html
{"cc_re__cc_re__cc_by_sa_3_0_en": ["class='cap-left'></div>\n<div class='cap-right'></div>\n</div>\n</div>\n<footer>\n<div class='footer-outer'>\n<div class='footer-cap-top cap-top'>\n<div class='cap-left'></div>\n<div class='cap-right'></div>\n</div>\n<div class='fauxborder-left footer-fauxborder-left'>\n<div class='fauxborder-right footer-fauxborder-right'></div>\n<div class='region-inner footer-inner'>\n<div class='foot section' id='footer-1'><div class='widget HTML' id='HTML7'>\n<div class='widget-content'>\n<div style=\"text-align:center;\">\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/deed.en_US\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by-sa/3.0/88x31.png\" /></a>\n\n</div>\n<div style=\"text-align:center;\">\nThis work by <a cc=\"http://creativecommons.org/ns#\" href=\"http://linkedin.com/in/shivanathd\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Shivanath D</a> is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/deed.en_US\">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.\n</div>", "='HTML7'>\n<div class='widget-content'>\n<div style=\"text-align:center;\">\n<a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/deed.en_US\"><img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by-sa/3.0/88x31.png\" /></a>\n\n</div>\n<div style=\"text-align:center;\">\nThis work by <a cc=\"http://creativecommons.org/ns#\" href=\"http://linkedin.com/in/shivanathd\" property=\"cc:attributionName\" rel=\"cc:attributionURL\">Shivanath D</a> is licensed under a <a rel=\"license\" href=\"http://creativecommons.org/licenses/by-sa/3.0/deed.en_US\">Creative Commons Attribution-ShareAlike 3.0 Unported License</a>.\n</div>\n</div>\n<div class='clear'></div>\n<span class='widget-item-control'>\n<span class='item-control blog-admin'>\n<a class='quickedit' href='//www.blogger.com/rearrange?blogID=7376572796327660789&widgetType=HTML&widgetId=HTML7&action=editWidget&sectionId=footer-1' onclick='return _WidgetManager._PopupConfig(document.getElementById(\"HTML7\"));' target='configHTML7' title='Edit'>\n<img alt='' height='18' src='http://img1.blogblog.com/"], "copyright__copyright__copyright": [" instances where a method behaviour is not\u00a0straightforward\u00a0to reproduce and hence test. These should be properly commented.\nResources :\nhttp://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods\nhttp://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm\u00a0(test assertions and debug/logging)\nhttp://blogs.developerforce.com/engineering/2013/04/apex-test-code-segregation.html\nhttp://www.laceysnr.com/2013/04/three-reasons-you-should-write-proper.html", " instances where a method behaviour is not\u00a0straightforward\u00a0to reproduce and hence test. These should be properly commented.\nResources :\nhttp://wiki.developerforce.com/page/An_Introduction_to_Apex_Code_Test_Methods\nhttp://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_system.htm\u00a0(test assertions and debug/logging)\nhttp://blogs.developerforce.com/engineering/2013/04/apex-test-code-segregation.html\nhttp://www.laceysnr.com/2013/04/three-reasons-you-should-write-proper.html"]}