Skip to content

[apex] New rule: Detect usage of @IsTest(critical=true) / @IsTest(testFor='...') annotations (RunRelevantTests, Beta, API 66.0+) #6988

Closed

Description

@tprouvot

Proposed Rule Name: ApexUnitTestClassShouldHaveRunRelevantTestsAnnotation

Proposed Category: Best Practices

Description:

Salesforce introduced two new (Beta, API v66.0+) modifiers for the @IsTest annotation, used together with the RunRelevantTests deployment test level:

  • @IsTest(critical=true) — guarantees the test class always runs during a RunRelevantTests deployment, regardless of the payload.
  • @IsTest(testFor='ApexClass:Name, ApexTrigger:Name, ...') — guarantees the test class runs whenever the deployment includes new/modified versions of the referenced Apex classes/triggers.

Reference: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_isTest.htm

Without one of these modifiers, a test class relies entirely on Salesforce's automatic dependency detection to decide whether it runs during a RunRelevantTests deployment. For test classes covering critical paths, or classes that exercise code reached only through dynamic mechanisms (e.g. Type.forName, metadata-driven routing/dispatch), automatic detection can miss the dependency, and the test class may silently be skipped during a deployment even though the code it protects changed.

This rule follows the same "should have" pattern as ApexUnitTestClassShouldHaveRunAs and ApexUnitTestClassShouldHaveAsserts, and flags test classes that use neither critical nor testFor on their @IsTest annotation, prompting the author to consciously decide whether the class needs one of these guarantees.

Important: since critical and testFor are only available starting with API version 66.0, classes compiled/deployed with a lower API version don't have access to these modifiers at all and should not be flagged by this rule. The rule should only consider classes whose API version (metadata apiVersion, as already surfaced on the Apex AST for other version-gated rules) is >= 66.0.

Code Sample: This should include code, that should be flagged by the rule. If possible, the "correct" code according to this new rule should also be demonstrated.

// Flagged (API version >= 66.0): no 'critical' or 'testFor' modifier
@IsTest
private class OpportunityEODTest {
    @IsTest
    static void validatesOrgWideBehavior() {
        // ...
    }
}

// Not flagged (API version < 66.0): modifiers aren't available yet
@IsTest
private class LegacyTest {
    @IsTest
    static void testSomething() {
        // ...
    }
}

// Good: explicitly marked as always relevant
@IsTest(critical=true)
private class OpportunityEODTest {
    @IsTest
    static void validatesOrgWideBehavior() {
        // ...
    }
}

// Good: explicitly tied to its dependencies
@IsTest(testFor='ApexClass:InvoiceEngine, ApexTrigger:InvoiceTrigger')
private class InvoiceDynamicFlowTests {
    @IsTest
    static void validatesDynamicInvocationPath() {
        // ...
    }
}

Activity

  1. added
    a:new-rule
    on Aug 19, 2026
  2. changed the title [-][apex] New rule: detect usage of @IsTest(critical=true) / @IsTest(testFor='...') annotations (RunRelevantTests, Beta, API 66.0+)[/-] [+][apex] New rule: Detect usage of @IsTest(critical=true) / @IsTest(testFor='...') annotations (RunRelevantTests, Beta, API 66.0+)[/+] on Aug 22, 2026
  3. added this to the 7.28.0 milestone on Sep 21, 2026
  4. added a commit that references this issue on Sep 21, 2026
  5. added a commit that references this issue on Sep 21, 2026
    6b4a2cf
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    a:new-rule

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions