Loading

Salesforce Apex Unit Testing Best Practices and Code Coverage Requirements

Veröffentlichungsdatum: May 1, 2026
Beschreibung

This article explains Salesforce Apex unit testing requirements, best practices for writing effective test classes, and how to check code coverage before deploying to production.

Salesforce requires all deployed Apex code to have unit tests that cover at least 75% of the code. When developing or deploying Apex code, you may ask yourself:

  • Why is my code coverage showing less than 75%?
  • How can I increase my code coverage?
  • What are Salesforce's best practice suggestions for good Apex unit tests?

 

Lösung
 

This article addresses Apex code coverage requirements and testing best practices that help ensure successful deployments to Salesforce production environments.

Code Coverage

  • You must have at least 75% of your Apex covered by unit tests to deploy your code to production environments.
  • All triggers must have at least one line of test coverage.
  • Salesforce recommends that you have 100% of your code covered by unit tests, where possible.
  • Calls to System.debug are not counted as part of Apex code coverage in unit tests.

Note: While only 75% of your Apex code must be covered by tests, the focus should not be on the percentage of code that is covered. Instead, make sure that every use case of your application is covered, including positive and negative cases, as well as bulk and single records. This approach typically leads to 75% or more code coverage.

Testing Best Practices

  • Test the classes in your application individually. Never test your entire application in a single test.
  • In the case of conditional logic (including ternary operators), execute each branch of code logic.
  • Make calls to methods using both valid and invalid inputs.
  • Tests should complete successfully without throwing any exceptions, unless those errors are expected and caught in a try…catch block.
  • Always handle all exceptions that are caught, instead of merely catching the exceptions.
  • Exercise bulk trigger functionality — use at least 20 records in your tests.
  • Use the ORDER BY keywords to ensure that the records are returned in the expected order.
  • Do not assume that record IDs are in sequential order. Record IDs are not created in ascending order unless you insert multiple records with the same request. For example, if you create Account A and receive ID 001D000000IEEmT, then create Account B, the ID of Account B may or may not be sequentially higher.

Setting Up Test Data

  • Create the necessary data in test classes so the tests do not have to rely on data in a particular organization.
  • Create all test data before calling the startTest method.
  • Since tests do not commit to the database, you do not need to delete any data after your test completes.
  • Write comments stating not only what is supposed to be tested, but the assumptions the tester made about the data and the expected outcome.

Apex Test Methods and Annotations

  • Use the isTest annotation for every test. Classes defined with the isTest annotation do not count against your organization limit of 3 MB for all Apex code.
  • Use the runAs method to test your application in different user contexts.
  • Use System.assert methods to prove that code behaves properly.

Checking Code Coverage

  • In Setup, search for "Apex Classes" in Quick Find. This shows the calculated code coverage for your org's unmanaged code.
  • In the Developer Console, there is a code coverage column showing coverage information broken down by individual class and trigger.
  • Clicking on the coverage percentage in the Developer Console highlights covered lines in blue and uncovered lines in red. It also displays the number of times a particular line is executed by a test.

Running Multiple Tests

  • In the Salesforce user interface, you may need to test the classes in your organization individually, instead of trying to run all tests at the same time by clicking Run All Tests.

Install Salesforce CLI
Testing with SFDX

Nummer des Knowledge-Artikels

000385650

 
Laden
Salesforce Help | Article