Apex Unit Tests
Last updated
Last updated
To facilitate the development of robust, error-free code, Apex supports the creation and execution of unit tests. Unit tests are class methods that verify whether a particular code is working correctly.
Salesforce recommends the following components that need to be tested:
Single Records: This includes testing to verify that a single record produces the correct, expected result.
Bulk Records: Any apex code, whether a trigger, a class, or an extension, may be used for 1 to 200 records. We must test not only the single record case but the bulk cases as well.
Positive scenarios: This type of component testing expects a system to save a record without error.
Negative scenarios: This type of component testing expects a system to give an error.
Restricted User: Test whether users with restricted access to the objects used in the code see the expected behavior, i.e., whether they can run the code or receive error messages.
The following test options are available when you deploy or commit or use CI jobs:
No Test Run:
No tests are run. This test level applies only to deployments to development environments like Sandbox, Developer Edition, or trial organizations. This test level is the default for development environments.
Run Specified Tests:
Only the tests that you specify are run. Provide the names of test classes in a comma-separated list.Make sure for the runTests parameter, and you're specifying the test class names separated by ",". The runTests parameter will be used only when the test level is set to Run Specified Tests
.
Code coverage requirements differ from the default coverage requirements when using this test level. The executed tests must cover each class and trigger in the deployment package for a minimum of 75% code coverage. This coverage is computed for each class and trigger individually and differs from the overall coverage percentage.
Run Local Tests:
All tests in your organization are run, except the ones that originate from installed managed packages. This test level is the default for production deployments that include Apex classes or triggers.
Run All Tests In Org:
All tests are run. The tests include all tests in your organization, including tests of managed packages.
Run Tests Based On Changes:
This option will identify apex test classes from your source package in addition to the default configured apex classes and run the identified tests to the destination environment. Also, if you want to include the newly identified apex classes from the packages in your default apex test class configuration list, please check the "Do you want us to update the test classes
" checkbox.
Points to Note:
Only CI Jobs have the "Do you want us to update the test classes
" checkbox enabled. This feature is yet to be implemented in other modules.
With our current "Run Tests Based on Changes" functionality, we still expect a test class for a wrapper class with appropriate code coverage, even if the wrapper class doesn't contain any executable code. Otherwise, we will encounter a code coverage error:
Code Coverage Failures: Class: TransactionHistoryActionParameters -- Test coverage of selected Apex Class is 0%, at least 75% test coverage is required *********** DEPLOYMENT FAILED ***********
User Salesforce Defaults:
Keeps the default behavior for all tests. In the sandbox, no tests are executed. All local tests are executed in the production if they contain Apex classes or triggers. Local tests are all tests, except the ones that originate from managed packages. If your package doesnât contain Apex components, no tests are run.
Point to Note:
Please make sure to execute all apex tests before configuring this option. This allows you to configure the mapping between the main class and the test class.
If you have cleared the last run history in your destination org, you must again execute run all tests. If not done, the dependent test execution will fail.
If you have refreshed your sandbox, then again you are required to execute run all tests. If not done, the dependent test execution will fail.
If the test classes do not exist in the package, the test level is configured based on the Run local Tests.
In the Submit for Validation
screen, select the Validate Deployment
checkbox and choose an org to validate your commit. Next, choose the test level of validation from the Apex Test Level
dropdown.
In the New Merge
screen and under the Prevalidate Merge
section, select the Validate Deployment
checkbox and choose an org to validate your merge. Next, choose the test level of validation from the Apex Test Level
dropdown.
In the Deployment Settings
screen, you can choose the apex test level to validate the deployment.
You can set the apex test level when creating or editing a CI job in the New/Edit CI job screen under the Deploy
section.
For more information on Apex unit tests, Refer to the Salesforce Trailhead module on Apex test level testing.