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.
The answer is B. Answer A just moves the logic outside of the test class, but still runs the same amount of code. When you could do is use a TestDataFactory class marked with @IsTest and put that in the @TestSetup method, especially if more than one test class will be using the same data.
Its B, because the problem is "many methods to create many records", if we use testSetup dont need to create many methods and the performance is increased. TestDataFactyory is used for reuse record creation in many classes, for example, create Account, Contact records, and reuse in many classes. But the question is not asking about reuse in other classes, so TestSetup its enough.
A voting comment increases the vote count for the chosen answer by one.
Upvoting a comment with a selected answer will also increase the vote count towards that answer by one.
So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.
rajesh4794
Highly Voted 2 years, 10 months agoamerbearat
Highly Voted 2 years, 3 months agolorenac2
Most Recent 11 months, 1 week agoDean5555
1 year, 1 month agolevian
1 year, 1 month agoChiaSam
1 year, 5 months agosinghkar
2 years, 5 months ago