Prevent trigger from running in single test

The only way to do this would be to code the functionality into your trigger. You could make use of static variables to prevent the trigger from running. Just create some class

public class StaticTest{
    public static Boolean doNotRunTrigger = false;

    public static void setDoNotRunTrigger(){
        doNotRunTrigger = true;
    }

    public static Boolean shouldRunTrigger() {
        return !doNotRunTrigger;
    }
}

And in your trigger

if(StaticTest.shouldRunTrigger()){
    //TriggerLogic
}

Then in your Test Class, just call the StaticTest.setDoNotRunTrigger() at the beginning of the test method and the trigger won't run!


I really liked @Bradley's approach however found it more logical to place the overwrite on the trigger instead.

In your trigger you would do something like below:

public with sharing class ApprovalTriggerHandler {
    public static Boolean bBypassTrigger = false; // set to `false` to skip  trigger code 
                                                  // execution, ex. during unit testing
    ...
    public static void onBeforeInsert(List<cms__Approval__c> oApprovalList) {   
      if (ApprovalTriggerHandler.bBypassTrigger) { return; }
      ...
    }
    ...
}

And in your test class you would disable running of triggers as follows:

private class ApprovalSchedulabe_Test {

    static testMethod void smokeTest() {
        ApprovalTriggerHandler.bBypassTrigger = true; // disable running of trigger code
        ....
    }
    ...
}