+ Start a Discussion
Sharath ChandraSharath Chandra 

Execution sequence of test methods.

if I have 5 test methods and a test setup method in a test class and when we perform a test run on this class, in what sequence will the test methods execute during the testing?.
Doug BeltowskiDoug Beltowski
The test setup method will run first before any test method.  I'm not sure in what order the individual test methods will run.
Swaraj Behera 7Swaraj Behera 7
Hi Sharath,
Below is a sample test class.
public class TestClass1{
  private static testMethod void testMethod(){
    // call method1
    // call method2
  private static void method1(){
   //method 1 code
  private static void method2(){
   //method 2 code


This way you can guarantee that method1 is called before method2. 
It does not matter the order in which they are called as each it a seperate transaction.
If you need the results fro the first method to be consumed in the second method, you will have to put the code together in the same method.

Swaraj Behera
NagendraNagendra (Salesforce Developers) 
Hi Sharath,

I agree with swaraj.If you really need the results to be continued from first  method and then second method you have to put the code at one place with in the same method.

The best way to understand the execution context is just assume a static apex method behaviour. Static methods don't have any state. Every thing executed within method is state full until the method exits.

Same thing happens in test classes everything executed between test methods block remain only for single transaction. Once the last execution finished everything reset to default. The only difference is test methods don't commit anything physically to database.

But during execution make sure that default behaviour of "run all test" is it executes all test methods in parallel so if there is a shared static method that query or insert record in object or custom setting then you may get exception due to conflicts. 

Please mark it as solved if it helps you.

Best Regards,