top of page

Test.isRunningTest in Apex

In Apex we can check if code is being called from an apex test class by using the method Test.isRunningTest().

Test.isRunningTest() returns true if the code is being executed by code in a test method otherwise it returns false. This lets us run code differently for real use cases and unit Tests.

//Check if a test is running
if(Test.isRunningTest()) {
//Run specific logic for Tests
//Check if test is NOT Running
//Run specific logic that wont run in tests

A quick list of scenario's where the Test.isRunningTest() can be used are,

  1. Testing HTTP Requests/Callouts to the external systems.

  2. To bypass the error log creation in unit tests to improve performance.

  3. To disable the triggers to avoid hitting the governor limits.

Something to keep an eye on when using the isRunningTest() is Code Coverage. You will not get code coverage for any code that's not covered in your unit tests. Therefore, if you overuse the isRunningTest() your code coverage may be in danger of falling below 75%.

191 views2 comments

Recent Posts

See All


Unknown member
Aug 10, 2021

Its really nice, very much detail is someone like me to start from basics


Detail explanation for all entry level salesforce developer who is finding hard time with unit test cases Very much appreciated🏆

bottom of page