In order to protect shared resources, Salesforce enforces a maximum number of SOQL queries, DML queries, ``++@future++`` method, etc... execution inside a single https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_transaction.htm[transaction]. This is part of https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Governor limits].


Every test method should check how close the tested code is to reach the governor limits. This is done by calling ``++Test.StartTest()++`` before the tested code and ``++Test.StopTest()++`` after it.


This rule raises an issue when a test method, i.e. a method annotated with ``++@isTest++`` or ``++testmethod++``, does not contain a call to ``++Test.StartTest()++`` and ``++Test.StopTest()++``.


== Noncompliant Code Example

----
@isTest()
public class MyTestClass {
    @isTest()
    static void myTestMethod() { // Noncompliant
        // Preparing test (creating records, etc...)
        // test code with no reference to Test.StartTest() or Test.StopTest()
    }

    static testmethod void myTestMethod2() { // Noncompliant
        // Preparing test (creating records, etc...)
        // test code with no reference to Test.StartTest() or Test.StopTest()
    }

    static void myHelper() {
        // ...
    }
}
----


== Compliant Solution

----
@isTest()
public class MyTestClass {
    @isTest()
    static void myTestMethod() {
        // Preparing test (creating records, etc...)
        Test.StartTest();
        // test code with no reference to Test.StartTest() or Test.StopTest()
        Test.StopTest();
    }

    static testmethod void myTestMethod2() {
        // Preparing test (creating records, etc...)
        Test.StartTest();
        // test code with no reference to Test.StartTest() or Test.StopTest()
        Test.StopTest();
    }

    static void myHelper() {
        // ...
    }
}
----


== Exceptions

No issue will be raised if the test class, i.e. the class annotated with ``++@isTest++``, contains helper methods, i.e. methods not annotated with ``++@isTest++`` or ``++testmethod++``, which contain calls to ``++Test.StartTest()++`` or ``++Test.StopTest()++``. This indicates that the test code has been factorised and the rule would raise false positives.


== See

* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_gov_limits.htm[Execution Governors and Limits]
* https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm[Using Limits, startTest, and stopTest]

