Print this page

Test.isRunningTest()

Knowledge Article Number 000205831
Description 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(). 
Resolution Setup up the trigger by leveraging isRunningTest(). 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. 

Example:
if(Test.isRunningTest()){
//do something
}

Other Usage scenarios:
1) To ensure the trigger doesn't execute the batch if Test.IsRunningTest() is true, and then test the batch class with it's 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. 




promote demote