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:
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.@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 } }
@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!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!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); }
3.144.114.85