Salesforce’s Summer '24 update brings an exciting new feature that enhances the tracking of Apex test setup methods using the ApexTestResult and ApexTestRunResult objects. This feature is available in all Salesforce editions and significantly improves the visibility and management of Apex test setup methods.
In this blog, I will guide you through how to use these new features to monitor and optimize Apex test setup methods, ensuring your tests run efficiently. Whether you're troubleshooting slow test runs or just want more granular control over your test setup performance, these insights will be invaluable.
Why Monitor Test Setup Methods?
Efficient testing is key to maintaining and deploying your Salesforce applications without disrupting operations. In previous versions, Salesforce only allowed monitoring of cumulative run time and system limits by parsing the debug logs. This method made it difficult to analyze the exact time spent in test setup versus actual test execution.
Salesforce’s Summer '24 update changes this by introducing new columns in the ApexTestResult and ApexTestRunResult objects. These new fields allow you to monitor the following:
Setup Methods: The IsTestSetup field in ApexTestResult identifies test methods annotated with @testSetup, distinguishing them from other test methods.
Setup Time: The TestSetupTime field in ApexTestRunResult tracks the cumulative time spent on all setup methods for a test run.
This new functionality lets you analyze the time spent on test setup separately from actual test execution, giving you the insights you need to improve performance.
Step-by-Step Guide: Monitoring Setup Methods
Let’s walk through creating an Apex test class, running the test, and monitoring the setup methods using the Salesforce Developer Console.
Step 1: Create an apex test class
The @testSetup annotation creates test data that is shared across all test methods in the class. This reduces redundancy, minimizes execution time, and ensures that common test data is only created once.
Here’s an example of an Apex test class with a @testSetup method:
I have created an apex test class with the name TestApexSetupMethods
@isTest
public class TestApexSetupMethods {
@testSetup
static void setupTestData() {
Account acc = new Account(Name = 'Test Account');
insert acc;
}
@isTest
static void testMethod1() {
Account acc = [
SELECT
Id,
Name
FROM
Account
LIMIT 1
];
System.assertEquals('Test Account', acc.Name);
}
@isTest
static void testMethod2() {
Account acc = [
SELECT
Id,
Name
FROM
Account
LIMIT 1
];
System.assert(acc != null, 'Account should not be null');
}
}
Explanation:
The @testSetup method creates an Account record that is shared across all test methods (testMethod1 and testMethod2).
By using @testSetup, we reduce the need to recreate this test data in each test method, leading to cleaner and more efficient tests.
Step 2: Test the test class
To test the Apex test class open the developer console >> Select file >> click open >> search the apex test class name TestApexSetupMethods >> click the run test button
Afterward, go to Tests >> check whether the test class methods are passed or failed. if all the methods are passed then move to the next step.
Step 3: Lets run the query to get the result
Select id, AsyncApexJobId, IsTestSetup, MethodName, Outcome, RunTime from ApexTestResult
What You’ll See:
IsTestSetup: This column will be true for any method that was run as part of the @testSetup annotation. This allows you to distinguish between test setup methods and actual test methods.
RunTime: The time taken by the test method to execute.
MethodName: The name of the test method or setup method executed.
In the output, we can see the IsTestSetup column for setup methods shows true as a result.
How is this useful?
Let’s consider a scenario where you have a large Salesforce application with hundreds of test classes and thousands of test methods. Some tests are taking longer than expected to run, but you can't determine if it's due to the setup or actual test execution.
Using the IsTestSetup field in ApexTestResult and TestSetupTime in ApexTestRunResult, you can now:
Identify slow setup methods: If TestSetupTime is disproportionately high, you can review and optimize your @testSetup methods to reduce their execution time.
Distinguish setup from test method issues: If setup methods are fast, but actual test execution is slow, you know where to focus your optimization efforts.
By breaking down test execution into these parts, developers can better benchmark and improve the overall performance of test classes.
Conclusion
The new Salesforce Summer '24 feature significantly enhances the monitoring of Apex test setup methods. With the new IsTestSetup column in ApexTestResult and the TestSetupTime column in ApexTestRunResult, developers can better understand and optimize their test performance.
By tracking setup time separately from test execution time, you can reduce bottlenecks, identify performance issues more easily, and ensure your test classes run as efficiently as possible.
Next Steps:
Implement @testSetup in your Apex test classes to reduce redundancy.
Run the SOQL queries provided to monitor your test setup and performance.
If you'd like to see the code and resources used in this project, you can access the repository on GitHub. To access the AVENOIRBLOGS repository, click here. Feel free to explore the code and use it as a reference for your projects.
Thank You! You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
Reference
Blog Credit:
S. Dash
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
Comentários