Activity Forums Salesforce® Discussions What is the use of test.isrunningtest() in Salesforce?

  • Anuj

    Member
    July 31, 2020 at 1:40 pm

    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, Batch Job etc. Returns true if the code being executed is invoked from a test class otherwise returns a false.

  • Shweta

    Member
    July 31, 2020 at 2:33 pm

    test.isrunningtest(): Using this method if you need to run different code depending on whether it was being called from a test.
    When you were using object but cannot be used in a test, or doing some Http requests. In such cases, there is no option, considered using " Test.isrunningTest() ". And also this Static method allows you to discover the code that was run from the test method. it returns hardcoded String instead of calling HTTP request and parsing the body and a fixed array of objects from a method

  • Kirandeep

    Member
    July 31, 2020 at 3:31 pm

    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 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.

  • Pooja

    Member
    July 31, 2020 at 5:27 pm

    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, Batch Job etc.

  • Sumit

    Member
    August 3, 2020 at 4:54 am

    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, Batch Job etc. Returns true if the code being executed is invoked from a test class otherwise returns a false.

Log In to reply.

Popular Salesforce Blogs