Testing best practices
I want to finish this chapter with some general testing best practices that developers should aim to follow when writing their test methods. We will cover scenario-specific best practices as we work through examples throughout the rest of the book, but these best practices will help your test code be more useful in validating the behavior of the code you have written and verifying that it will not exceed governor limits, whatever the scenario. The first best practice I want to discuss is the use of the Test.startTest()
and Test.stopTest()
methods.
Using Test.startTest and Test.stopTest to demarcate the code under test
In both of the preceding code snippets, we have used the Test.startTest()
and Test.stopTest()
methods before and after our callout code. Our callout code is the code we are testing and, as such, should be enclosed within these methods to have its own governor context and be demarcated. Calling Test.startTest()
within a test method provides...