PDI · Question #107
A developer is writing tests for a class and needs to insert records to validate functionality. Which annotation method should be used to create record for every method in the test class?
The correct answer is C. @TestSetup. The @TestSetup annotation method should be used to create common test records once for all test methods within a test class, optimizing test execution and setup time.
Question
A developer is writing tests for a class and needs to insert records to validate functionality. Which annotation method should be used to create record for every method in the test class?
Options
- A@isTest (SeeAllData-true)
- B@FreTest
- C@TestSetup
- D@StartTest
How the community answered
(22 responses)- A5% (1)
- B5% (1)
- C91% (20)
Why each option
The `@TestSetup` annotation method should be used to create common test records once for all test methods within a test class, optimizing test execution and setup time.
`@isTest(SeeAllData=true)` allows access to organization data, which is generally discouraged in unit tests for isolation and predictability; it does not create test data but exposes existing data.
`@isTest` (or `@Test`) is used to define a test class or test method, but `@TestSetup` is the specific annotation for setting up data that should be available to multiple test methods.
The `@TestSetup` annotation is specifically designed to create common test data that is inserted once and then available to all test methods in the test class, which is more efficient than creating data in each test method or using SeeAllData=true.
`@StartTest` marks the point where governor limits are reset, and processing time starts counting, but it does not create test records.
Concept tested: Apex test data setup
Source: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_testsetup_method.htm
Topics
Community Discussion
No community discussion yet for this question.