Tips and tricks for efficient testing

Following the general pattern for creating your own test data outlined here: executing the tested code between startTest() and stopTest() calls and always including asserts in the tests will set you on a good path for useful, robust tests. This doesn't, however, mean that the tests are fast. To help keep tests fast and your test-code feedback loop tight, keep these tips and tricks in mind:

  • If you don't need to, don't insert and query data to and from the database. Starting with winter '13, you can set a value for the Id field, so long as you don't try to insert it. This allows you to create an object, set its ID, and create other objects that reference it. The slowest part of any web-based application is historically the database, and Salesforce1 is no exception. If you can cut down your SOQL and DML, your tests will run faster.
  • Use @testVisible. This annotation allows you to quickly and easily annotate private class variables and methods and then access their values or execute the code during tests. Here's an example of it in use:
    public class exampleCode {
      // Private member variable
      @TestVisible 
      private static Boolean normallyHidden = true;
    
      // Private method
      @TestVisible 
      private static void cantSeeMe(Integer 3) {
        //do amazing things
      }
    }    
  • Find a general mocking library and use it. The more complex your org becomes, the longer it will take to run your tests. If you're mocking out objects and methods whose return values are crucial to the code being tested but not the tested code itself, you not only add stability to your tests, you know what the mock object or stubbed method will return each time! But, you also speed up your code by bypassing all of the code those objects would be running! Additionally, you should mock every single HTTP callout!
  • Avoid using @isTest(seeAllData=true). Here be dragons. This annotation allows your tests to view all of the data in your org. SOQL queries run in tests without the annotation cannot see your existing Accounts, Contacts, and so on. When you annotate testMethod with @isTest(seeAllData=true), all bets are off. It's true that some areas of the platform still require seeAllData=true because those objects are not able to be created in Apex code. For example, you cannot create an approval process in Apex. To test your approval process or the code that submits a record for approval, you'll still have to use @seeAllData=true. This also reinforces the idea that you should be creating all of the needed test data in your test methods!
  • Consider investing in a continuous integration system. Many Continuous Integration (CI) systems, such as Jenkins or drone.io, work well with the Salesforce1 platform. These systems run your tests for you, at periodic intervals or after specific events, like a Git commit. They help you keep pace with the other developers in your org and allow for integration sandboxes where your team's changes are merged together and the tests run. This helps you identify when another developer has made a change that breaks what you're working on before you try to deploy it!
  • Always test your code with bulk data, but change the volume of data per environment. Proving that your code is bulk safe doesn't have to happen in production. Create a custom setting for TestOptions and create a numeric field titled EnvironmentBulkSize. Reference that custom setting in your test cases as you can see in the following code. Remember to set your sandbox's EnvironmentBulkSize option to 200 but set your production value to something like 5. The less the work, the faster the tests:
    @isTest
    private class ExampleCodeTests {
      TestOptions__c options;
    
      @testSetup static void setup() {
        options = TestOptions__c.getInstance('default');
      }
    
      @isTest static void someTest(){
        Account[] accounts = (Account[])TestFactory.createSObjectList(new Account(), options.EnvironmentBulkSize);
      }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.114.85