Proactively Identifying Apex tests that are ticking time bombs

In the complex and dynamic world of Salesforce development, managing and optimizing Apex tests is a critical yet challenging endeavor. My journey into the heart of this challenge began when I noticed a recurring issue in our Salesforce org, which was laden with outdated and monolithic Apex tests. The problem? We were consistently hitting SOQL limits, causing disruptions and delays in our development cycle. The Salesforce platform enforces governor limits to ensure shared resources are used efficiently, and SOQL limits are a crucial part of this governance, restricting the number of queries that can be executed to maintain optimal performance.

Identifying the Problem

The root of our challenge lay in the legacy Apex tests, some of which were written years ago, under different assumptions about data volume and system complexity. With no immediate way to refactor all tests overnight and a continuous development pipeline to maintain, we needed a solution that was both strategic and scalable. The answer came in the form of an innovative, nightly automated test run system.

Implementing the Solution

Leveraging the Apex Limits class and its methods, such as getLimitQueries(), I devised a system to log the SOQL query count for each Apex test at the end of its execution. This process was automated to run nightly, ensuring that every test was evaluated for its SOQL usage without manual intervention.

Here’s a simplified example of how we implemented logging for SOQL limits:

private class ExampleApexTest {
    @isTest static void testMethod1() {
        // Setup and execute test logic here
        // Log the SOQL limit usage at the end of the test
        Integer soqlQueriesUsed = Limits.getQueries();
        Integer soqlLimit = Limits.getLimitQueries();
        System.debug('SOQL Queries Used: ' + soqlQueriesUsed + ' of ' + soqlLimit);

By embedding this snippet at the end of each test, we could capture and log the number of SOQL queries used, providing clear visibility into which tests were approaching their limits. Please note that you are welcome to log other types of Salesforce limits but for my use case, I was only concerned with SOQL hogs.

Daily Insights and Proactive Refactoring

This nightly logging system unveiled daily insights into our Apex tests’ SOQL usage, identifying the tests that were dangerously close to hitting their SOQL limits. With this data in hand, we could prioritize refactoring efforts, focusing on the most critical tests that threatened the stability of our Salesforce releases.

Given the vast number of legacy tests, it was impractical to refactor everything at once. Instead, this system allowed us to be strategic, targeting the “ticking time bombs” that posed the greatest risk to our development process.

The Impact: Stability and Proactivity

The implementation of the nightly automated test run system marked a turning point in our Salesforce development practices. By gaining a daily understanding of our tests’ SOQL usage, we transitioned from a reactive to a proactive approach. This shift not only allowed us to maintain the health and performance of our Salesforce org but also significantly improved the stability of our releases.

This proactive strategy has been instrumental in preempting potential disruptions, ensuring that our Salesforce environment remains robust and reliable. The ability to identify and address problematic tests before they impact our development cycle has been a game-changer, adding a layer of foresight and control to our process.


The journey from encountering frequent SOQL limit issues to establishing a stable and proactive development environment has been both challenging and rewarding. By leveraging Salesforce’s Apex Limits class and implementing a nightly automated test run system, we have transformed our approach to Apex test management. This innovative solution has not only enhanced the stability of our Salesforce releases but also set a new standard for managing legacy tests in a large and complex Salesforce org. The results speak for themselves: a more efficient, predictable, and resilient development process that stands the test of time and change.

Originally at: Proactively Identifying Apex tests that are ticking time bombs. | by SF SENSEI | Feb, 2024 | Medium

Leave a Comment