Skip to content

Instantly share code, notes, and snippets.

@brianmfear
Last active January 11, 2024 05:09
Show Gist options
  • Save brianmfear/835a7e1e57b2285a949d25cb8ee71d75 to your computer and use it in GitHub Desktop.
Save brianmfear/835a7e1e57b2285a949d25cb8ee71d75 to your computer and use it in GitHub Desktop.
Need more CPU or heap for creating your test data? Make it Queueable.
// Asking for extra CPU time and heap for those really "heavy" orgs
@isTest class GetExtraLimitsForYourUnitTest implements Queueable {
@testSetup static void testSetup() {
Test.startTest(); // TIP: this avoids governor limits for your @isTest methods
System.enqueueJob(new GetExtraLimitsForYourUnitTest()); // We'll get async limits!
// P.S. Did you know that the end of a unit test method triggers async code,
// just as if you called Test.stopTest()?
}
public void execute(QueueableContext context) {
// I now have 60000ms to do my setup, instead of just 10000ms.
// Also, double the heap, so 12mb to set up any data I want to use.
// Very useful if you need the time, and you don't need asynchronus code to run.
Account[] testAccounts = new Account[0];
for(Integer i = 0; i < 200; i++) {
testAccounts.add(new Account(Name='Test '+i));
}
insert testAccounts;
}
///////////////////////////// Do tests here
@isTest static void doTestOperation() {
// We can validate that no limits were used, but we keep our data.
Assert.isTrue(Limits.getCPUTime() < 1);
Assert.isTrue(Limits.getDmlStatements() == 0);
Assert.isTrue(Limits.getQueries() == 0);
Assert.isTrue(Limits.getQueueableJobs() == 0);
Assert.isTrue([SELECT COUNT() FROM Account] == 200);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment