Automated unit testing for Salesforce

Automated unit testing for Salesforce

Jason Mann on

Share with



Testing that your Apex classes work as expected is an important part of Salesforce development. But it’s also essential to keep testing the code in your production org. In this post, we’ll look at why automated unit testing for Salesforce really matters, and how you can set it up for your Salesforce org in next to no time at all.

Testing Apex isn’t a choice

We all know that you can’t write Apex without writing any tests at all. Salesforce enforces its 75% code coverage requirement by preventing you from deploying with insufficient test coverage. So not only is testing a good idea, you won’t be able to deploy code without also writing tests. How well those tests are written is another matter, but testing before deployments is well-known and widely adopted, with many teams striving for 100% code coverage.

Changes in your orgs can cause silent test failures

As well as testing before deployment, you should also test the code in production because changes to your org may cause a regression - your existing unit tests may begin to fail. For example, the interaction between workflows and Apex code means it’s easy to end up with a unit test that passes when deployed to production, then later fails when someone adds a new workflow or validation rule. When that happens, you need to know about it.

Unless you set up automated unit testing to detect these lurking test failures, they can lie hidden until it comes to deployment time - causing unexpected failures and a few extra gray hairs.

Gearset DevOps Lifecycle Summit 2025: What a complete DevOps view means for you

Find out more

Why in-org unit testing isn’t more common

According to our latest State of Salesforce DevOps survey, just 54% of teams use automated unit testing. Why do only half of teams keep testing their production code? Mainly, it’s because there’s no in-built test automation in Salesforce, so many teams decide not to re-run unit tests after a deployment, and it’s all too easy to forget about them altogether.

While the tests in your org are run whenever you deploy to production, there’s no easy way to manage testing in your orgs once the changes have been deployed. Testing can be manually initiated for individual orgs via the Apex Test Execution page, but if you have a complex org the process can take hours and it often falls down the priority list.

Salesforce's Apex Test Execution page

Test automation is the answer

Unit testing is a repeatable process that takes way too much time if done manually, which makes it a perfect candidate for automation. All Salesforce teams should aspire to running production tests on a regular cadence, with automatic reporting, failure tracking, and an easy way to keep on top of the latest results.

With Gearset’s automated unit testing solution, running unit tests is a breeze. All you need to do is choose an org and Gearset will automatically run all unit tests every 24 hours. You can choose how to get the notifications of test results: email, SMS, Slack or Chatter. Gearset even saves a full test history, so you can easily dive in to find exactly which tests failed and why.

Automated unit testing is a great way to boost deployment success rates and make sure you always understand the state of your orgs.

How to set up unit testing in Gearset

Setting up automated unit testing in Gearset takes just a couple of minutes.

  1. Navigate to the Unit testing page in Gearset.
  2. Click Add new job.
  3. Choose the time of day your unit tests will run, name the unit testing job, and select the org you want to set up the job for. You can also set your own code coverage target if you want to aim higher than 75%. Finally, you can include tests from namespaces by listing them.
Gearset's UI for creating a new unit testing job

Your new job will first run automatically at the scheduled time, and then every 24 hours. All results will be saved in your test history for easy access, and you’ll receive notifications of the overall test status (passed/failed). From the Unit testing dashboard, you can see all the relevant details of your production tests at a glance, including code coverage. You can also run all unit tests on demand at the click of a button.

Gearset's UI showing unit testing jobs

View test results and debug test failures

When your unit testing notification warns you of test failures, you’ll want to look at those and understand what’s going wrong. Gearset helps you debug these errors by letting you dig into the test results.

  1. From the Unit testing dashboard, click View history for your unit testing job. You’ll see a full test history for that org, showing you the test status at daily intervals, as well as code coverage stats. In this case, there’s a test that’s been failing for the last few days.

    Unit testing job history in Gearset's UI
  2. Click View results for the test run you want to explore in more detail. All your tests will be listed, along with their status.

  3. Find any failed tests and select them to view the error message and full stack trace, so you can understand what’s going wrong and where exactly you’ll find the issue in your code.

    Gearset UI showing test failures

Unit testing and Salesforce DevOps

DevOps is about seeing development, releases and monitoring as part of one continuous cycle, and testing should be used at each stage of the cycle. When you’re deploying code with Gearset, you can run unit tests as part of the process. Gearset will even detect which tests you should run! Then, with automated unit testing in your production org, you’ll be able to monitor your code in production and catch any regressions where tests begin to fail.

With testing at every step of the process, DevOps speeds up project delivery and improves release quality. This explains why Salesforce teams have consistently told us release quality is a key benefit of DevOps alongside improved productivity.

Another key principle of DevOps is that teams should be able to release on demand, so they can respond without delay to any situation that calls for a release. Some teams fall foul of Salesforce’s 75% code coverage threshold and suddenly discover they can’t deploy when it really matters. With automated unit testing, your team will see this coming and avoid being in a situation where you’re unable to release.

Automate your unit tests today!

Adding more automation to your development and release process is a powerful way to improve outcomes without needing any extra manual effort. There are plenty of ways you can introduce automation to your DevOps process - testing is just one good example.

It’s a real pain when test failures in production block a deployment. Don’t let code fail silently and have those test failures creep up on you. Set up your automated unit testing with Gearset and get full visibility into the status of your Apex. If you’re new to Gearset, unit testing is just one of the DevOps tools you can access for free in a 30-day trial. Why not get started today?

Try all of Gearset for free