A company notices that their unit tests in a test class with many methods to create many records for prerequisite reference data are slow. What can a developer to do address the issue?
- A. Move the prerequisite reference data setup to a TestDataFactory and call that from each test method.
- B. Move the prerequisite reference data setup to a @testSetup method in the test class.
- C. Move the prerequisite reference data setup to a static method in the test class and call that from each test method.
- D. Move the prerequisite reference data setup to the constructor for the test class.
What should be correct option - A or B
#Trailhead and why ?
Thanks for replying. I was going through the Documentation and came across this -
Test Data Factory - are excluded from the organization code size limit and execute in test context.
TestSetup Methods - Same.
Since both are annotated with Test Setup, so will be rolled back in the end.
And creating test data ( example - 5 accounts, or 10 Leads) can be done by both of them. We can use the same methods - but where to place it - in Same Test Class (@testSetup)or separately(Test Data Factory) ?
Common test utility classes are public test classes that contain reusable code for test data creation.
Public test utility classes are defined with the isTest annotation, and as such, are excluded from the organization code size limit and execute in test context. They can be called by test methods but not by non-test code.
The methods in the public test utility class are defined the same way methods are in non-test classes. They can take parameters and can return a value. The methods should be declared as public or global to be visible to other test classes. These common methods can be called by any test method in your Apex classes to set up test data before running the test. While you can create public methods for test data creation in a regular Apex class, without the isTest annotation, you don’t get the benefit of excluding this code from the organization code size limit.
Use test setup methods (methods that are annotated with @testSetup) to create test records once and then access them in every test method in the test class. Test setup methods can be time-saving when you need to create reference or prerequisite data for all test methods, or a common set of records that all test methods operate on.
Test setup methods can reduce test execution times especially when you’re working with many records. Test setup methods enable you to create common test data easily and efficiently. By setting up records once for the class, you don’t need to re-create records for each test method. Also, because the rollback of records that are created during test setup happens at the end of the execution of the entire class, the number of records that are rolled back is reduced. As a result, system resources are used more efficiently compared to creating those records and having them rolled back for each test method.
If a test class contains a test setup method, the testing framework executes the test setup method first, before any test method in the class. Records that are created in a test setup method are available to all test methods in the test class and are rolled back at the end of test class execution. If a test method changes those records, such as record field updates or record deletions, those changes are rolled back after each test method finishes execution. The next executing test method gets access to the original unmodified state of those records.