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
if(!Test.isRunningTest()){
//Run specific logic that wont run in tests
}
A quick list of scenario's where the Test.isRunningTest() can be used are,
Testing HTTP Requests/Callouts to the external systems.
To bypass the error log creation in unit tests to improve performance.
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%.
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🏆