Automatic unit test selection for Salesforce deployments

Automatic unit test selection for Salesforce deployments

Calvin Childs on

Share with



Salesforce deployments with Gearset can be up to 12x faster than with first-party tools, and we’re constantly making even more improvements to streamline your deployments and save even more of your time and effort. Currently, we’re piloting new functionality that automatically suggests which unit tests you should run when you manually deploy or validate a package in Gearset. This improvement will make test selection and testing quicker and easier. It should also save lots of teams from needing to track their unit tests outside of Gearset.

The importance of unit testing

We all know that Apex code can’t be developed in production. Apex classes and their test classes must be written in a developer environment and then deployed to production. Salesforce encourages unit testing and requires that your test coverage is at least 75%. If your code coverage falls below this threshold, Salesforce will not allow you to deploy to that org. Many Salesforce teams sensibly strive for better code coverage than 75%, and adopting a test-driven development (TDD) process is a good way to do this.

Using Gearset, you can set up unit testing jobs that automatically run the unit tests in an org every day. Automated unit testing allows you to track code coverage and alerts you to any tests that fail. Gearset also enables you to run unit tests during deployments to make sure that your deployment doesn’t break any tests. It’s better to understand your deployment package’s impact on your tests before deploying.

Glaziers Hall, London

DevOps Dreamin’ London 2024

Find out more

Running the right tests

When running unit tests during a deployment, you want to strike a balance. On the one hand, you need to run enough tests to cover the changes in the deployment package. On the other hand, you don’t want to slow down your deployment. Running all of your tests is an obvious way to make sure you’ve covered everything, but that can take a very long time - particularly if you have thousands of unit tests in your org. And most of those tests won’t be necessary to run with each deployment, so you’re needlessly wasting time.

Ideally, you identify the right tests to run during the deployment and only run those tests. Different sets of metadata changes will affect different tests, so working out which tests to run is something that needs to happen for each and every deployment package - and that’s a challenge.

Manually selecting tests

Until recently, the only solution in Gearset has been to choose tests manually, either by selecting from a list of all the unit tests in your deployment package and target org, or by entering a comma-separated list of all the tests you’d like to run.

Searching through a full list of tests wasn’t a great option for teams with a huge number of tests. For this reason, many of Gearset’s users preferred to specify tests using the textbox, and we have kept this option available alongside the improvement we’ve made. Just click on Manually specify tests and type or paste in the names of the test classes you want to run.

Screenshot of a comma-seperated list of unit tests

In many cases, teams have been maintaining a spreadsheet that maps out all their unit tests, using this spreadsheet to determine which tests they’ll need to run for each deployment. Some users have lists of commonly used tests that they use as templates they can paste into the textbox.

Many teams follow naming conventions for tests, for example, adding Test as a prefix or suffix to the name of the Apex class being tested. This can help to identify the tests that need to be run. But both of these methods for specifying tests involve manual work and take up time and brainpower. And needing to refer to lists of tests stored outside of Gearset doesn’t help to streamline the deployment process. For all of these reasons, we’ve worked on a new and improved way for you to select tests.

Automatic test selection

Our recent updates to this part of the Gearset app make the test selection process much easier. Where you used to see all the tests in your deployment package and target org in one long list, you’ll now see this has been split in two: a list of preselected tests that Gearset suggests you run; then the rest of your tests listed below, which by default are not selected.

The list of suggested tests is automatically generated based on the metadata in your deployment package. As well as any test classes in your package, Gearset also suggests you run tests that call any Apex classes in your deployment package. It only suggests test classes that have direct dependencies in your package, so chains of dependent tests won’t all be suggested. There may be additional tests you know you want to include, which you can do easily. This smart new functionality will save you from having to work out each and every test you should run and selecting them or pasting in a list of tests.

How to use automatic test selection

As before, you can select which tests to run by clicking on the arrow button next to either the Deploy now or Validate deployment button. Then click Specify tests to run.

Screenshot of the specify tests button

You can review the list of suggested tests and, if you feel it necessary, amend the selections if you prefer. You can deselect suggested tests and select other tests from the list below. Use the search bar to track down the tests you’re looking for.

Screenshot of preselected unit tests

In the example shown above, Gearset has picked up that the tests PasswordControllerTest, AccountSetupTest and ValidateUserTest are either in the deployment package or have dependencies in the package. Once you’re ready to deploy or validate your package and run these tests, clicking Run tests will begin the deployment or validation.

Get started today

If you’re looking to implement Salesforce testing best practices with Gearset’s automatic unit testing solution, you can start a free 30-day trial today.

Ready to get started with Gearset?