Please provide an example of when Test.isRunningTest() becomes true and Test.isRunningTest() becomes false? I am not able to understand this concept.

I have tried using Test.isRunningTest in an Apex class and it returns false, but the same method called from a test returns true.


The documentation explains:

Use this method if you need to run different code depending on whether it was being called from a test.

It is very rarely needed (and best avoided if at all possible) and when it is needed you add it to the production code not the test code.

One place I have consistently used it is to stop an error happening in tests where a field name is added to the standard controller:

public AccountDeepCloneController(ApexPages.StandardController sc) {
    this.sc = sc;
    if (!Test.isRunningTest()) {
        sc.addFields(new String[] {'Name'});

because otherwise addFields generates an error in tests.

Another place is when programmatically generating PDF data as again the normal call generates an error in tests:

Blob pdf = !Test.isRunningTest() ? pr.getContentAsPdf() : Blob.valueOf('Test content');

Another place is to put Messaging.SingleEmailMessage objects into a @TestVisible list instead of passing them to Messaging.sendEmail as email sends are awkward to test.

  • please explain with the small code to show the difference – Elavarasan Mar 31 '16 at 9:11
  • @Elavarasan There is not much more I can do here. When the product code is launched fro ma test Test.isRunningTest() returns true and when the product code is launched normally Test.isRunningTest() returns false. I've posted typical examples of the use of this in the answer. – Keith C Mar 31 '16 at 9:15

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.

for example

you cant send http request in test method so you have to provide result by this check

public class AuthCallout {

   public void basicAuthCallout(){
     HttpRequest req = new HttpRequest();

     String username = 'myname';
     String password = 'mypwd';
   string result;
     Blob headerValue = Blob.valueOf(username + ':' + password);
     String authorizationHeader = 'BASIC ' +
     req.setHeader('Authorization', authorizationHeader);

if (!Test.isRunningTest()) {

     Http http = new Http();
     HTTPResponse res = http.send(req);
     result = res.getBody());

     result = '{"widget":{"debug":"on","window":{"title":"Sample widget","name":"main_window","width":500,"height":500}}}';



here we check if code is not executed by test class it will make an http call and get result else we will provide demo result for further use checkout this link, it will explain you how Test.isRunningTest() is used,


  • Before the HttpCalloutMock mechanism was introduced this was the right choice, but a cleaner mechanism now for this situation is Testing HTTP Callouts by Implementing the HttpCalloutMock Interface. – Keith C Mar 31 '16 at 12:54
  • but if we need the response for further looping and record creation, i think this simple if else statement would be good enough as we can put the fake resonse over there no need to create an extra class for that and also cover our code coverage and its just an example i showed to use Test.isRunningTest – vishal chaudhary Apr 1 '16 at 10:26
  • Just making the point that (usually) HttpCalloutMock is the better way to go. – Keith C Apr 1 '16 at 11:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.