+ Start a Discussion
arun Bikkasaniarun Bikkasani 

what is the use of test.isrunningtest() method in test class

jigarshahjigarshah
The Test.isRunningTest()  method is used to identify, if the piece of code being executed is invoked from a Test class execution or from other artefacts such as a Trigger, Bacth Job etc. Returns true if the code being executed is invoked from a test class otherwise returns a false.

This method usually comes in handy, when you intend to conditionally restrict execution of certain Apex code blocks, based on whether they are being invoked from a test class or not. A practical example would be that performing web service callouts in Apex are not supported within Test Code. Hence you could use the Test.isRunningTest() to conditionally identify and route the execution of a code block that calls the Test Mock framework to simulate a mock, callout reponse.

The code block would look something like this.
if(Test.isRunningTest()){
   //Invoke mock web service response
}
else{
   //Invoke web service callout
}
Refer the following links to understand further about using Test.isRunningTest()
1. https://help.salesforce.com/articleView?id=000205831&type=1
2. https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_test.htm

Please do not forget to mark this thread as SOLVED and answer as the BEST ANSWER if it helps resolve your issue.
SoundarSoundar
Hi Arun Bikkasani,

As Short and Sweet .. We faced in some situation ,

1. you were using object but cannot be use in a tests, or doing some Http requests.
2. In Such case there is no option, considered using " Test.isrunningTest() "
3. This Static method allows you to discover the code was run from the test method.
           *#  return hardcoded String instead of calling http request and parsing the body
           * #  return a fixed array of objects from a method  

Example:- 
 
Boolean error = testParsesResponse(response); 
if (Test.isRunningTest()) { 
  error  = false; 
} if (error) { 
  alert('There is Some Problem Please Clean Up');
​ }

the Unit test will never get into the error condition if(error), Because it's force to True in all unit Test's...


Cheers !!!

You can ask me if any doubts (Feel Free)...

Regards,

Soundar Raj 


 
Nishant_SFDC DeveloperNishant_SFDC Developer
Thanks, Jigar it is very helpful.
Deepali KulshresthaDeepali Kulshrestha
Hi Arun,

Test.isRunningTest() :-

Returns true if the currently executing code was called by code contained in a test method, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.

Other scenarios:
1) To ensure the trigger doesn’t execute the batch if Test.IsRunningTest() is true, and then test the batch class with its own test method.
2) Testing callouts – in your callout code you check to see if you’re executing within a unit test context by checking Test.isRunningTest() and instead of getting your callout response from an HttpResponse.send() request, you return a pre-built test string instead.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.
SFDC TortoiseSFDC Tortoise
When we are setting up test data from Apex test method, we need a way to disable the triggers that will fire. It might cause "LimitException: "Too many SOQL queries: 101". In this case, the triggers are not the target of the test case, hence this scenario will cause the test method to fail. It is not necessary to disable the trigger for every test case, instead we can use isRunningTest(). 
SFDC TortoiseSFDC Tortoise
When we are setting up test data from Apex test method, we need a way to disable the triggers that will fire. It might cause "LimitException: "Too many SOQL queries: 101". In this case, the triggers are not the target of the test case, hence this scenario will cause the test method to fail. It is not necessary to disable the trigger for every test case, instead we can use isRunningTest(). 

Returns true if the currently executing code was called by code contained in a test method, false otherwise. Use this method if you need to run different code depending on whether it was being called from a test.