Sub topic 7
Creating and Running Flow Tests
This chapter assumes you can build a record-triggered flow in Flow Builder and know basic Salesforce objects and fields. You also need a sandbox or Developer Edition org. No Apex is needed here.
What a flow test is
A flow test is a saved check that runs a record-triggered flow against sample data and compares the result to what you expect. Flow Builder supports tests for record-triggered flows that are autolaunched, meaning they run in the background with no screen. Other flow types need the debugger or Apex tests, which later chapters cover.
A test runs inside a transaction that Salesforce rolls back at the end. No records from the test are saved, so you can run it in a sandbox without cleaning up.
How a test is built
Each test has three parts.
- Initial record. For a "create" flow, this is the record that triggers the flow. For an "update" flow, it is the record's state after the change. You can also set the record's values before the change.
- Updated fields. For an update flow, these are the field values you change to trigger the flow.
- Assertions. These are the conditions that must be true when the flow finishes. If any assertion is false, the test fails.
Creating a test
Use this example. An Opportunity flow creates a follow-up task when the stage changes to Closed Won.
- Open the flow in Flow Builder and save it at least once.
- Click Tests in the toolbar, then New.
- Enter a test name and description, such as
Closed Won creates task. - Set the initial record values. Give it a name, a close date, and a stage of
Prospecting. - Set the updated field values. Set
StageNametoClosed Won. - Add assertions. Under the flow's variables, check that the follow-up task record exists, or that a field the flow sets has the expected value.
- Save the test.
Choose values that match your flow's entry conditions. If the test data does not meet them, the flow does not run, and your assertions fail for the wrong reason.
Running tests and reading results
From the Tests panel, run one test or Run All. Each test shows Pass or Fail. Open a failed test to see which assertion failed, the expected value, and the actual value. You can also open the flow's execution details to see which elements ran, as you do in the debugger.
Tests run against the flow version you have open. This lets you test a draft version before you activate it.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Every assertion fails | Test data does not meet the entry conditions | Match the trigger object, field values, and criteria |
| Test cannot be created | Flow type is not supported | Use a record-triggered autolaunched flow, or use Apex |
| Test fails on a validation rule | Initial record breaks a rule | Supply values that satisfy required fields and rules |
| Test passes but production fails | Test covers one record and one user | Add tests for edge values, and use bulk and Apex tests |
| Test fails after editing the flow | Assertion refers to a renamed variable | Update the assertion |
Test paths, not only the happy path
Write at least one test for each decision outcome. For the example flow, cover these cases:
- The stage changes to Closed Won, so the task is created.
- The stage changes to Closed Lost, so no task is created.
- The stage is already Closed Won and another field changes, so no second task is created.
Name each test after the behavior it checks. When a test fails later, the name tells you what broke.
Running tests before you deploy
Flow tests deploy with the flow as metadata. Before you activate a new version, run all tests on that version. Run them again after any change to related objects, validation rules, or other automation, because those can change the result without you editing the flow.
Exercise
A flow test for an update-triggered flow fails every assertion, but the flow works when you edit a record by hand. What do you check first?
Show answer
Check that the test's initial and updated field values satisfy the flow's entry conditions. If they do not, the flow never runs in the test, so the assertions fail. Also check that the initial record satisfies required fields and validation rules.
Exercise
Name two things a Flow Builder test cannot show you about a flow.
Show answer
It does not show behavior with many records in one transaction (limit problems), and it does not cover flow types other than record-triggered autolaunched flows. Bulk tests and Apex tests cover these.