Apex Unit Tests
  • 15 Nov 2022
  • 3 Minutes to read
  • Contributors
  • Dark
    Light

Apex Unit Tests

  • Dark
    Light

What Are Apex Unit Tests?

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 piece of code is working properly.

What to Test in Apex?

Salesforce recommends the following components that need to be tested:

  1. Single Records: This includes testing to verify that a single record produces the correct, expected result
  2. 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.
  3. Positive scenarios: This type of component testing expects a system to save a record without error.
  4. Negative scenarios: This type of component testing expects a system to give an error.
  5. Restricted User: Test whether a user with restricted access to the objects used in code sees the expected behavior, i.e whether they can run the code or receive error messages.

Choose Which Tests to Run

The following test options are available when you deploy or commit or use CI jobs:

  • NoTestRun— No tests are run. This test level applies only to deployments to development environments, such as sandbox, Developer Edition, or trial organizations. This test level is the default for development environments.
  • RunSpecifiedTests— 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, 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. Each class and trigger in the deployment package must be covered by the executed tests for a minimum of 75% code coverage. This coverage is computed for each class and trigger individually and is different than the overall coverage percentage.
  • RunLocalTests— 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.
  • RunAllTestsInOrg— 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 would like 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.
    Point 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 yet.
  • Salesforce Default— Keeps the default behavior for all tests. In the sandbox, no tests are executed. In the production, all local tests are executed 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:
  1. 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.
  2. 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.
  3. 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.
  4. If the test classes do not exist in the package, the test level is configured based on the Run local Tests.

Where can I choose the Apex Tests level in the AutoRABIT application

During EZ-commit validation

In the Submit of 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.

During Merge Prevalidation

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.

During Deployment

In the Deployment Settings screen, you can choose the apex test level to validate the deployment.

During Continuous Integration (CI) Jobs

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.


Was this article helpful?