We have designed tests to work on collection of records so we can ensure Governor Limits are not violated. These tests have a surprising behaviour:
- They always succeed on Scratch orgs
- They fail on Sandboxes only when all the tests are run in parallel with an APEX CPU Timeout Exceeded
Interesting enough, the exception is not raised inside the Test.startTest() / Test.stopTest() region but in the preceding code that sets up a complex requirement graph before the test is started.
This fact raises a number of questions:
- Is this behaviour well known and should be taken into consideration when designing tests?
- Should @testSetup always be used when possible, since a new set of governor limits is available inside the method?
- When it is not possible to use @testSetup because different test methods inside the same class require different set-ups, is it better to
- Give up the idea of having multiple test scenarios for the same feature in the same class, and start breaking it down
- Use Test.startTest() and Test.stopTest() not to wrap only the "effectful" code whose outcome you wish to test, but the whole testmethod body