To verify the functionality of your Apex code, Gearset allows you to run your Salesforce unit tests when you deploy changes to your orgs. There are four different test levels you can choose from.
Apex test levels
Salesforce default tests: In a sandbox, no tests are executed. In production, all local tests are executed if your deployment package contains Apex classes or triggers. Local tests are all tests, except those that originate from managed packages. If your deployment package does not contain Apex components, no tests are run.
Don't run any tests: No tests are executed, unless you are deploying to production and your deployment package contains Apex classes or triggers, in which case the default tests are run.
Run only your tests: All tests in your organization are run, except those that originate from managed packages.
Specify tests to run: Only the tests that you specify are run. You must specify names of test classes in a comma-separated list. Code coverage requirements differ from the default coverage requirements when using this level in production. The executed tests must cover the classes or triggers in your deployment package with a minimum of 75% code coverage. This coverage is computed for each class or trigger individually and is different from the overall coverage percentage. The pre-populated apex tests are either in the deployment package or have dependencies in the package.
Note: UI tests will be triggered after a successful deployment. The results will not be shown in Gearset, you'll need to check your provider for details.
Specifying the test level in Gearset
Validations: To run the default test level, click the main
VALIDATE DEPLOYMENT
button on the pre-deployment summary. To specify the other test levels, click the arrow to the right of the button and choose the test level for the validation.
β
Deployments: To run the default test level, click the main
DEPLOY NOW
button on the pre-deployment summary. To specify the other test levels, click the arrow to the right of the button and choose the test level for the validation.
Continuous integration: You can set the test level when creating or editing a CI job in the
Test settings
tab. If you make changes to the test level of an existing job, they will be applied the next time the job runs.
βWhen selecting the option to
Specify tests to run
, a few more granular options will appear. This will enable you to maximize the chances of running all the relevant tests and achieving acceptable code coverage.
βTest classes & Test Suites: Define a manual list of individual test classes or test suites to run. Each test class or suite name should be separated by a comma.
Tests that reference Apex changes: instruct Gearset to run tests that have a compile-time reference to Apex changes included in each CI job run's package. If you change or add new Apex tests in your package, those will also be included.
Tests that match naming conventions: instruct Gearset to run tests that match naming conventions of Apex changes included in each CI job run's package.
To understand more about the latter two options, here's an article explaining how Gearset selects which tests to run.
A note on the formatting of test classes for managed packages
If you're needing to run test classes out of a managed package, as part of the deployment of some unmanaged code, you'll need to specify them as "namespacehere.ClassNameHere
". Using a period rather than an underscore. This is verified as per this Salesforce documentation.
For more information on Apex unit tests, see this Salesforce Trailhead module on testing: https://trailhead.salesforce.com/en/modules/apex_testing/units/apex_testing_intro