- The Apex testing framework enables you to write and execute tests for your Apex classes and triggers on the Lightning Platform.
- Apex Unit tests ensure high quality for your Apex code and let you meet the requirements for deploying Apex.
- Apex code can only be written in a Sandbox environment or a Developer org, not in Production.
- Apex code can be deployed to a production org from a sandbox. Also, app developers can distribute Apex code to Customers from their Developer orgs by uploading packages to the Lightning AppExchange.
- In addition to being critical for quality assurance, Apex unit tests are also requirements for deploying and distributing Apex.
Benefits of Apex Unit Tests
- Ensuring that your Apex classes and triggers work as expected.
- Having a Suite of regression tests that can be rerun every time classes and triggers are updated to ensure that future updates you make to your app don’t break existing functionality.
- Meeting the code coverage requirements for deploying Apex to production or distributing Apex to Customers via packages.
- High-quality apps are delivered to the production org, which makes production users more productive.
- High-quality apps delivered to package subscribers, which increase your customers trust.
Code Coverage Requirement for Deployment
- Before you can deploy your code or package it for the Lightning Platform AppExchange, at least 75% of Apex code must be covered by tests, and all those tests must pass.
- In addition, each trigger must have some coverage. Even though Code coverage is a requirement for deployment, don’t write tests only to meet this requirement.
- Make sure to test the common use cases in your app, including positive and negative test cases, and bulk and single-recording processing.
Important to Remember
- Calls to System.debug are not counted as part of Apex code coverage.