Use of Flow Test vs Test Class for Flow in Salesforce

Use of Flow Test vs Test Class for Flow

Introduction of Flow Test

Previously, if we wanted to test flow, we needed to write an Apex test class for the flow. With the Summer ’22 release Salesforce introduced a testing framework for record-triggered Flows to automate the testing of Flows. Now we can let Salesforce test the Flow, ensure that the results are as per the expectation and highlight when they are not. We can now create & run tests within the Flow builder itself – declaratively without writing any code.

How it works

Set Test Details, Trigger, and Path

Provides a flow test label and a description of the test. Set the test trigger to Created / updated according to scenario. Path of test is sets to Run Immediately.

image.png Set Initial / Updated Triggering Record

Using the search function, we are able to search for the record to use as a starting template. Upon record selection, the fields are populated from the selected record. The test inherits the field values from the selected record. However, we can modify these field values before finalizing the test. The test is not linked to this record but contains and uses its field values.

image.png Set Assertions

Assertion is a way to compare the actual outcome with the predicted outcome. If they match, then assertion evaluates to true. Otherwise, the assertion fails. Write the following assertions to ensure the record entry criteria for the flow are met and the expected outcome is achieved.

image.png Run test and View Details

Click on the “View Tests” button, select the dropdown for the flow test we wants to run, and select Run Test and View Details.

image.png The flow test runs and highlights the path the test took. If we needs to test the flow, we can just run the test. No more setting up the test data programmatically. This increases testing efficiency! If we need to make an enhancement to this flow in the future, we will need to update the associated test record accordingly.

image.png Benefits

  1. Easy and convenient to use – We can create test records through UI and put it under the test.
  2. Time and effort – It reduces the time and effort required as we don’t need to write code but just create records from UI and put assertion.
  3. It won’t impact existing code coverage on class level – If we migrate any functionality from trigger to flow, the underlying logic from the trigger / class also cleaned manually as part of this exercise. This won’t reduce code coverage for the class as the logic is taken out entirely.
  4. Flow Test coverage – Coverage percentage is not captured in flow test like we can do it through apex class. Rather it is capable of covering nodes. Once we select “Run Test and View Details”. The path highlighted in yellow will depict the actual coverage of flow from start to end node.

image.png

image.png

image.png Disadvantage

  1. You can only create flow tests for create, update, or create/update record-triggered flows. Deleting record-triggered flows are not yet supported. – For only delete record scenario we can write test class
  2. Can impact overall coverage – Since overall test coverage is calculated based on the test coverage of all the classes combined (average) so if we migrate any feature which has higher test coverage in the relevant apex class, it might reduce the overall code coverage percentage. (as currently we are only focusing on migrating trigger related to account to flow only 1 test class is impacted so there is no significate change in apex code coverage)
  3. Flow tests don’t support flow paths that run asynchronously. However, we don’t have async flow in our org. We are creating flows to cover scenarios which run synchronously.

Recommendation

  • We can start taking step towards moving to flow test as Salesforce continuously making improvements to flow so in future releases, we can expect flow test can come with more improvements and reduce any limitations.
  • We can start by writing flow test for Fast Field Updates (before) record trigger Flows.
  • Enhance test coverage of classes which have less coverage so that we can use Flow test.

FAQ

What is coverage percentage to deploy Flow?

In relation with the Apex tests, flow tests are not considered part of test coverage as in the 75%+ for prod deployment.

Do they provide code coverage like test class does? If not we need to think of removing flow from test coverage?

Yes they do coverage by highlighting node by node.

Can you confirm if flow test allows run bulk test?

No it does not support bulk record testing. In most of trigger related scenarios, we have created individual record in test class and checking the test scenarios. If we replace those triggers with flows it wont be a big difference in test cases.

Does flow test support asynchronous?

Flow tests don’t support flow paths that run asynchronously. However, we don’t have async flow in our org. We are creating flows to cover scenarios which run synchronously.

What is performance of flow tests compared to test classes?

I don’t see any official documentation but I believe flow test will be faster as compared to test class because when we run a test it will instantiate the class and call individual methods. We can run individual test cases using flow test but it is not possible to run a single individual method from test class leading to lot of time.

Reference

Flow Test (beta)

Considerations for testing flow

 

Did you enjoy this article?
Signup today and receive free updates straight in your inbox.
I agree to have my personal information transfered to MailChimp ( more information )
50% LikesVS
50% Dislikes