function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Shaijan ThomasShaijan Thomas 

starttest and stoptest is not resetting the limits, see the following code


@isTest(SeeAllData=True)
public class TestStatAndStop {
    public static testmethod void MyMothod()
    {
        for (integer i =1; i<=90;i++)
            Contact b1 = [select id from contact limit 1];
        Test.startTest();
        for (integer i1 =1; i1<=99;i1++)
        {
            Account b = [select id from account limit 1];
        }  
        Test.stopTest();
}
}
Best Answer chosen by Shaijan Thomas
James LoghryJames Loghry

Strange.  I just tried this on my dev org and also got a limit exception thrown.

Generally speaking, unit tests are written to call external methods / code and test them, not to test arbitrary executions or SOQL queries.  That being said, I updated your class to call a method, and the governor limit resolved itself:

@isTest(SeeAllData=True)
public class TestStartAndStop {
    public static testmethod void MyMethod(){
        query();
        Test.startTest();
        query();
        Test.stopTest();
	}

	public static void query(){
		for(Integer i=0; i < 99; i ++){
			Account a = [Select Id From Account Limit 1];
		}
	}
}

Furthermore, it appears if you call any external method, even Limits.getQueries(), the governor limits are reset.

All Answers

Grazitti TeamGrazitti Team

Hi,

Why are you querying inside loop. Do not write any query inside for loop , it will definitely hit limit.

Regards,
Grazitti Team
Web: www.grazitti.com
Email: sfdc@grazitti.com
Shaijan ThomasShaijan Thomas
Issue is not that one. This just a test purpose. Issue is governer limits are not reseting inside startTest and StopTest
 
James LoghryJames Loghry

Strange.  I just tried this on my dev org and also got a limit exception thrown.

Generally speaking, unit tests are written to call external methods / code and test them, not to test arbitrary executions or SOQL queries.  That being said, I updated your class to call a method, and the governor limit resolved itself:

@isTest(SeeAllData=True)
public class TestStartAndStop {
    public static testmethod void MyMethod(){
        query();
        Test.startTest();
        query();
        Test.stopTest();
	}

	public static void query(){
		for(Integer i=0; i < 99; i ++){
			Account a = [Select Id From Account Limit 1];
		}
	}
}

Furthermore, it appears if you call any external method, even Limits.getQueries(), the governor limits are reset.
This was selected as the best answer
Shaijan ThomasShaijan Thomas
Thanks Loghry, I was trying it based on the above code.