Blog

Salesforce Apex Unit Testing with FlexDeploy

Testing is a critical component of a successful, long-term development process for Salesforce. Apex supports the creation and execution of unit tests to facilitate the development of robust, error-free code. Unit tests are class methods that verify whether a particular piece of code is working properly.

With FlexDeploy, you can leverage the Salesforce API to verify the functionality of Apex code by executing configured unit tests. Running these unit tests as part of your development and deployment pipeline increases the quality of deployments, and helps to catch errors early — before they become big problems.

In this blog, we will look at the options available with regards to the test execution and how this can be combined with Salesforce Deployments.

FlexDeploy and Apex Unit Test Levels

With FlexDeploy, Apex unit tests are available in 4 distinct execution levels:

  • NoTestRun: No tests are run. This test level is the default for development environments.
  • RunSpecifiedTests: Only the tests specified are run. Code coverage requirements differ from the default coverage requirements when using this test level.
  • RunLocalTests: All tests in your org are run, except the ones that originate from installed managed and unlocked 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 org, including tests of managed packages.

The Test Level can be set either in the inputs for the deploySalesforce Salesforce plugin operation or at the package level.

Define and Configure a Package

You can use the Packages tab in your FlexDeploy project to create new packages or view previously existing packages. To get started, select an existing package or create a new one using the steps described in Project Packages and then add the Salesforce objects you would like to include in the Package.

Configure Apex Unit Test Levels

At the individual package level, you can configure the tests to run for a particular package by setting the Test Level and the Tests properties.

Configure Apex tests to run for a particular package by setting the Test Level and the Tests properties.

At the Workflow level, the tests to be executed can be configured for all execution by settings the Artifact Test Level and Artifact Tests plugin inputs.

Configure Apex tests to be executed for all deployments by settings the Artifact Test Level and Artifact Tests plugin inputs.

The standard approach is going to be to set the Test Level at the package level for each package so that only a subset of the unit tests are executed as required. This can be used to exercise fine-grained control over the Apex Units executed for each package. Based on your requirements, you have the option to configure the Test Level and the Tests at the Workflow level which will cause the same settings at the package level to be overridden during runtime and this can be used to enforce a common level for each environment.

switch(FD_ENVIRONMENT_CODE) {
  case ['DEV','TEST']: // Pass multiple environments
    return 'RunLocalTests';
  case 'QA':
    return 'RunSpecifiedTests';
  case 'PROD':
    return 'RunAllTestsInOrg';
  default:
    return 'NoTestRun';
}

The Test Level input in the workflow can be set to something similar to the example shown above to have the executions follow a given level as per the environment.

Executing Apex Unit Tests

Once the Package and the Workflow are configured as required, we can proceed with the Deployment execution to run the unit tests and deploy the artifact. When a Deploy execution is triggered, FlexDeploy attempts to deploy the changes. After this is completed successfully, the configured tests are executed.

View the deployment details to see the success of configured Apex unit tests.

The Apex Test Results page in Salesforce Setup shows all the test cases that were executed as a part of the deployment.

The Apex Test Results page in Salesforce Setup shows all the test cases that were executed as a part of the deployment.

If you wish to only run the tests without deploying the changes, you can do so by using the validateSalesforce plugin operation in the workflow. With this configuration, the changes will only be validated in Salesforce when the deployment is triggered in FlexDeploy. When you are ready, the validated package can then be quickly deployed using the commitPackage operation.

When you are ready, validated package can be quickly deployed using a FlexDeploy operation.

After the execution is completed, the Test Summary section in the Summary tab of the execution result shows the number of successful and failed tests in a concise manner.

FlexDeploy's execution results shows the number of successful and failed tests in a concise manner.

The Test Results tab goes into further detail about each of the tests executed like the Duration and Message in case of a failure.

FlexDeploy offers additional detail about each of the Apex tests executed like the Duration and Message in case of a failure.

Details from the test execution, like counts for Test Failures, Test Run, Code Coverage, etc. can be stored as outputs and processed further as required.

Details from Apex test execution, like counts for Test Failures, Test Run, Code Coverage, etc. can be stored as outputs and processed further as required.

When a Pipeline/Release flow is used in FlexDeploy to orchestrate Salesforce changes across environments, the Test gate can be used in a Stage to verify the test execution results from a previous environment and have the snapshot proceed if certain qualifiers are met.

Set a Test gate in your FlexDeploy pipeline to have a snapshot proceed if certain qualifiers are met.

The Test gate can be clicked on to show the test results directly from the Release Dashboard without having to navigate to a different screen.

A Test gate can be clicked on to show the test results directly from the Release Dashboard without having to navigate to a different screen.

 

Conclusion

FlexDeploy makes it very easy to integrate your Apex Unit Tests configured in Salesforce to the deployment process, allowing you to be in line with DevOps best practices. With FlexDeploy, you can set up a comprehensive DevOps solution for your Salesforce application.

Leave a Comment

Your email address will not be published.

Scroll to Top