Gearset suggested Apex tests
David Emmerson avatar
Written by David Emmerson
Updated over a week ago

Manual compare and deploy

When deploying or validating a package to Salesforce using Gearset, you're able to specify which Apex test classes you want Salesforce to execute as part of validating the package.

When you choose this option, Gearset will list all the available test classes that are contained in the target org or in the package itself.

Here, because my package included the Apex class named MyClass, Gearset suggests that I probably want to run MyClass_T. It has based this on the _T suffix, which is one of a few patterns used to suggest tests based on naming convention. This selection logic also now applies to Apex Triggers in the package.

The feature to automatically detect relevant tests is actively being developed, with the name-based suggestions being the most recent update. If you encounter any situations where Gearset isn't suggesting a test that you would have expected, please let us know via the in-app chat and we'll make improvements where possible.

Continuous integration jobs

It's also possible to run test with increased granularlity when setting up a CI job. Select the Specify tests to run option when creating or editing your CI jobs.

Once selected, you can manually define specific test class or suites to run alongside some of the options that instruct Gearset to add some tests based on the package your CI runs deploy or validate. This will enable you to maximize the chances of running all the relevant tests and achieving acceptable code coverage.

There are two options for getting Gearset to include tests based on the package for your CI job runs:

  1. Tests that reference Apex changes in the deployment

  2. Tests that match naming conventions for Apex changes in the deployment

Let's look at these options and their criteria one by one.

Tests that reference Apex changes

Under this option Gearset will include a test if:

  • You're making a change to that test in your package

  • Deploying it as a new test in your package

  • That test contains a reference to Apex that's changed in your package

When Gearset attempts to provide these test suggestions, it makes a call to the Salesforce API, and requests a list of all Apex classes and their associated symbol tables. These tables are used to determine if there are any test classes in the org that have a reference to one of the Apex classes being deployed.

Known issues and limitations

  1. If Salesforce is already compiling Apex when Gearset requests the symbol tables, it will return null. This occasionally results in no tests being suggested, which in turn would mean 0% code coverage and a failed validation.

    To mitigate the issue, Gearset retries a number of times (currently 10). There is an increased delay between each try, to avoid any regularly timed compilations that might be happening in the org.

    There is no workaround should Gearset reach the end of the retries, but the naming convention test will still apply, as the class names are retrieved even when the symbol tables are not received.

  2. Gearset only takes account of direct references between the test file and the class it is testing. If you have situations where you expect the inclusion of class A to cause test class C to be run when C only interacts with A through one or more intermediate classes, then a reference to A would need to be added to C.

    Example

    1. MyClass is the only class included in a deployment.

    2. IntermediateClass uses MyClass.

    3. IntermediateClassTests are tests that target IntermediateClass.

Tests that match the naming convention

In practical terms, making sure that the test class matches the naming convention is one of the easiest ways to ensure that a test class will be suggested.

If MyClass is included in the package, the naming convention will include any tests with the following names:

  1. MyClass_Test

  2. MyClass_Tests

  3. MyClassTest

  4. MyClassTests

  5. MyClass_T

These matches are done case-insensitively, e.g. Gearset will include MyClassTEST.

These naming conventions apply to Apex classes and Apex triggers. So, for example, if MyTrigger is included in the package, then having a test named MyTriggerTests will ensure that the test will be suggested.

Future improvements

Gearset development is guided by our customers, so please let us know if you have any suggestions about how we could improve this feature.

Did this answer your question?