How do you unit test Salesforce Flows? Now you can create, save, and run flow tests in Flow Builder. Each time you modify the flow, you can run the test and assert the result. This Conceptually Similar to Apex Test Classes. Let see how you can test your Salesforce flow using Flow Tests.
What is Flow Tests?
Flow Tests is a Declarative Framework to Test Salesforce Flows. You can create Flow Tests declaratively, execute with the click of a button. It was beta released in Summer ‘22, GA from Winter ‘23.
Flow Tests is similar to Apex Test Classes. But does not require development / coding skills.
Steps to Create Flow Tests
- Set Test Details, Trigger, and Path,
- Set Initial Triggering Record,
- Set Assertions
Learn more here.
What is Assertion in flow?
Assertion in tests refers to the validating that the outcome is as expected. For example.
- Flow should set the Discount % to 10 for Gold Tier Customers with Opportunity Amount < 100,000
- In Flow Test, we will set assertion to confirm that when an Opportunity is created for Gold Customer the Discount % is set to 10
create & Run Tests for Salesforce Flows Video
How to create & Run Tests for Salesforce Flows Declaratively. Unit test for Flow.
Check Ashish Agarwal Step by Step Guide to Automate Testing of Salesforce Flows Declaratively guide.
Benefits, Limitations & Observations
Why You Should Consider Flow Tests?
- Makes Flows Robust & Reliable
- Reduces the Testing Effort by Users
- ROI multiples with every testing iteration
Limitations on Flow Tests
Here are some limitation of Flow tests.
- Limit: The maximum number of tests per flow is 200
- Record Triggered Flow Only: Available only for record-triggered flows.
- No Support for Delete: Flow tests don’t support flows that run when a record is deleted.
- Positive Testing only: It only support positive testing.
- Test coverage: Flow tests don’t count towards flow test coverage requirements.
- Flow Versions: It independent of flow versions. Saving flow as New Flow will not copy Flow tests.
Observations
Deployment of Flow Tests
FlowTest available as separate metadata component for deployment. FlowTest need s to be deployed separately than flows. This will not run automatically on deployment. It supports by Change Set & Packages. You can use Salesforce CLI (SFDX Commands) to retrieve & Deploy.
sfdx force:source:retrieve -m "FlowTest"
sfdx force:source:deploy -m "FlowTest"
Summary
Salesforce flow is becoming mature day by day. Unit testing for flows should be added a Best practices for Salesforce Flows.
During our testing of flow, I saw some scenarios that we encountered where we needed to create like contact, account, etc. and refer in the flow test but In the flow test, I don’t see that happen it always takes a record created in a sandbox which will not be passed if move the flow test from one sandbox to another envrionment as contact not existing any suggestion.